dotnet/reactive · error · ArgumentOutOfRangeException

nameof(index)

Error message

nameof(index)

What it means

This is an ArgumentOutOfRangeException thrown by AsyncObservable.ElementAtOrDefault when the index parameter is negative. Even though the operator yields a default value instead of an error for out-of-range indexes on a live sequence, the index itself must still be a non-negative ordinal, so it is validated synchronously at the call.

Solutions

  1. Clamp or validate: if (index < 0) handle separately before calling
  2. Convert sentinels like -1 explicitly (e.g. take the last element via a different operator)
  3. Fix the upstream computation that produced the negative index

Example fix

// before
var item = source.ElementAtOrDefault(idx); // idx = -1 sentinel
// after
var item = idx >= 0 ? source.ElementAtOrDefault(idx) : default(TSource);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));
var result = source.ElementAtOrDefault(index);

Type guard

bool IsValidElementIndex(int i) => i >= 0;

Try / catch

try { var result = source.ElementAtOrDefault(index); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index") { /* clamp index or use default */ }

Prevention

When it happens

Trigger: Calling source.ElementAtOrDefault(n) with n < 0; commonly from a parsed/decimal index that lost its sign check, or a computed value like pos - 1 where pos == 0.

Common situations: Mapping external indexes (UI selection, API parameters) directly into ElementAtOrDefault without clamping; arithmetic that underflows; treating -1 as a sentinel for 'last' which this API does not support.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/503d5389244ab41f. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs:14

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> ElementAtOrDefault<TSource>(this IAsyncObservable<TSource> source, int index)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            return Create(
                source,
                index,
                static (source, index, observer) => source.SubscribeSafeAsync(AsyncObserver.ElementAtOrDefault(observer, index)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> ElementAtOrDefault<TSource>(IAsyncObserver<TSource> observer, int index)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            return Create<TSource>(

View on GitHub (pinned to 94b5d5ab91)