dotnet/reactive · error · ArgumentOutOfRangeException

Value cannot be null. (Parameter 'count')

Error message

Value cannot be null. (Parameter 'count')

What it means

The Window(source, timeSpan, count, scheduler) overload throws ArgumentOutOfRangeException when count is less than or equal to zero (runtime message: 'Value cannot be null. (Parameter count)'). Each window must hold at least one element, so non-positive counts are invalid and rejected before the operator is constructed.

Solutions

  1. Pass a count of at least 1.
  2. Enforce positive batch size when reading configuration (fail fast with a clear config error).
  3. Normalize computed sizes: count = Math.Max(1, computed).

Example fix

// before
var windows = events.Window(TimeSpan.FromSeconds(10), settings.MaxItems, scheduler);
// after
if (settings.MaxItems <= 0) throw new InvalidConfigException("MaxItems must be positive");
var windows = events.Window(TimeSpan.FromSeconds(10), settings.MaxItems, scheduler);
Defensive patterns

Strategy: validation

Validate before calling

if (count <= 0) throw new ArgumentException("window count must be >= 1", nameof(count));
count = Math.Max(1, count);
var windows = source.Window(timeSpan, count, scheduler);

Type guard

static bool IsValidWindowSize(int n) => n > 0;

Try / catch

try { var windows = source.Window(ts, count, sched); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* fix batch size source and retry */ }

Prevention

When it happens

Trigger: Calling source.Window(timeSpan, count, scheduler) with count <= 0, often a batch-size setting of 0 or a computed size from an empty collection.

Common situations: Config file with PageSize=0; division-based sizing rounding down to 0; uninitialized int fields; tests parameterizing count as 0 to check edge behavior.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs:126

            if (timeSpan < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(timeSpan));
            if (count <= 0)
                throw new ArgumentOutOfRangeException(nameof(count));

            return CreateAsyncObservable<IAsyncObservable<TSource>>.From(
                source,
                (timeSpan, count),
                static (source, state, observer) => WindowAsyncCore(source, observer, (o, d) => AsyncObserver.Window(o, d, state.timeSpan, state.count)));
        }

        public static IAsyncObservable<IAsyncObservable<TSource>> Window<TSource>(this IAsyncObservable<TSource> source, TimeSpan timeSpan, int count, IAsyncScheduler scheduler)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (timeSpan < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(timeSpan));
            if (count <= 0)
                throw new ArgumentOutOfRangeException(nameof(count));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            return CreateAsyncObservable<IAsyncObservable<TSource>>.From(
                source,
                (timeSpan, count, scheduler),
                static (source, state, observer) => WindowAsyncCore(source, observer, (o, d) => AsyncObserver.Window(o, d, state.timeSpan, state.count, state.scheduler)));
        }

        public static IAsyncObservable<IAsyncObservable<TSource>> Window<TSource, TWindowBoundary>(this IAsyncObservable<TSource> source, IAsyncObservable<TWindowBoundary> windowBoundaries)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (windowBoundaries == null)
                throw new ArgumentNullException(nameof(windowBoundaries));

            return CreateAsyncObservable<IAsyncObservable<TSource>>.From(
                source,

View on GitHub (pinned to 94b5d5ab91)