dotnet/reactive · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values…

Error message

Specified argument was out of range of valid values. (Parameter 'count')

What it means

ArgumentOutOfRangeException for 'count' in AsyncObservable.Take: the requested number of elements is outside the valid range (negative counts are rejected; zero is allowed and yields an empty sequence). The guard fires synchronously at query construction, before any subscription.

Solutions

  1. Clamp the count to zero or more: Math.Max(0, count)
  2. Validate the input value before calling Take
  3. Fix the upstream arithmetic that produced the negative number

Example fix

// before
var page = source.Take(total - offset); // can be negative
// after
var page = source.Take(Math.Max(0, total - offset));
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
var taken = source.Take(count);

Type guard

bool IsValidCount(int count) => count >= 0;

Try / catch

try { var taken = source.Take(count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* clamp and retry */ }

Prevention

When it happens

Trigger: Calling source.Take(-1) or any negative count, often from a computed value such as pageSize - skip that went negative.

Common situations: Paging logic where the remaining-item calculation underflows (e.g. take = total - offset with offset > total), or unvalidated user input for a limit.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Take.cs:19

// 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. 

using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;

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

            if (count == 0)
            {
                return Empty<TSource>();
            }

            return CreateAsyncObservable<TSource>.From(
                source,
                count,
                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.Take(observer, count)));
        }

        public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (duration < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(duration));

View on GitHub (pinned to 94b5d5ab91)