dotnet/reactive · error · ArgumentOutOfRangeException

count

Error message

count

What it means

Window(source, timeSpan, count, scheduler) uses count as the maximum number of elements per window and requires it to be strictly positive. When count is zero or negative it throws ArgumentOutOfRangeException immediately. A window must be able to contain at least one element for the operator to be meaningful.

Solutions

  1. Pass a count >= 1; validate before calling Window
  2. Fix the config or computation that yielded 0/negative (e.g. Math.Max(1, computedCount))
  3. If you intended a time-only window, use the Window(source, timeSpan, scheduler) overload that has no count

Example fix

// before
var windows = source.Window(TimeSpan.FromSeconds(1), batchSize, Scheduler.Default); // batchSize == 0
// after
var windows = source.Window(TimeSpan.FromSeconds(1), Math.Max(1, batchSize), Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count), "Window count must be at least 1.");

Try / catch

try { var windows = source.Window(ts, count, scheduler); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { log.Error("Invalid window count", ex); }

Prevention

When it happens

Trigger: Calling Window with count = 0, a negative count, or a count computed from an empty/uninitialized configuration value (e.g. int parsed from empty string defaulting to 0).

Common situations: Batch-size settings read from config defaulting to 0, off-by-one arithmetic producing 0, division-based sizing (totalItems / groups) that rounds to 0 for small totals.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs:2426

        /// Specifying a TimeSpan.Zero value for <paramref name="timeSpan"/> is not recommended but supported, causing the scheduler to create windows as fast as it can.
        /// Because all source sequence elements end up in one of the windows, some windows won't have a zero time span. This is a side-effect of the asynchrony introduced
        /// by the scheduler, where the action to close the current window and to create a new window may not execute immediately, despite the TimeSpan.Zero due time.
        /// </remarks>
        public static IObservable<IObservable<TSource>> Window<TSource>(this IObservable<TSource> source, TimeSpan timeSpan, int count, IScheduler 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 s_impl.Window(source, timeSpan, count, scheduler);
        }

        #endregion

        #endregion
    }
}

View on GitHub (pinned to 94b5d5ab91)