dotnet/reactive · error · ArgumentOutOfRangeException

throw new ArgumentOutOfRangeException(nameof(count));

Error message

throw new ArgumentOutOfRangeException(nameof(count));

What it means

The timeSpan/count Buffer overload throws ArgumentOutOfRangeException when count is zero or negative. count is the maximum number of elements per buffer and must be a positive integer. Validation is performed eagerly at the call site.

Solutions

  1. Pass a positive integer for count (>= 1)
  2. Validate/clamp the batch size before calling: if (count < 1) count = 1;
  3. Fix configuration loading so unset batch sizes get a sane default instead of 0

Example fix

// before
int count = config.BatchSize; // could be 0
var buffered = source.Buffer(TimeSpan.FromSeconds(1), count);
// after
int count = Math.Max(1, config.BatchSize);
var buffered = source.Buffer(TimeSpan.FromSeconds(1), count);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 1) throw new ArgumentOutOfRangeException(nameof(count));
// or clamp: count = Math.Max(1, count);

Try / catch

try { var b = source.Buffer(timeSpan, count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { count = 1; /* retry with default */ }

Prevention

When it happens

Trigger: Calling Observable.Buffer(source, timeSpan, count) with count <= 0 — e.g. a batch-size config of 0, an empty page-size, or integer arithmetic that produced 0 or a negative number.

Common situations: Batch-size settings read from config files where 0 means 'unbounded' in user code but is invalid here; division results rounding to zero; uninitialized int fields defaulting to 0.

Related errors


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

Appendix: source

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

        /// Specifying a TimeSpan.Zero value for <paramref name="timeSpan"/> is not recommended but supported, causing the scheduler to create buffers as fast as it can.
        /// Because all source sequence elements end up in one of the buffers, some buffers 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 buffer and to create a new buffer may not execute immediately, despite the TimeSpan.Zero due time.
        /// </remarks>
        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, TimeSpan timeSpan, int count)
        {
            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));
            }

            return s_impl.Buffer(source, timeSpan, count);
        }

        /// <summary>
        /// Projects each element of an observable sequence into a buffer that's sent out when either it's full or a given amount of time has elapsed, using the specified scheduler to run timers.
        /// A useful real-world analogy of this overload is the behavior of a ferry leaving the dock when all seats are taken, or at the scheduled time of departure, whichever event occurs first.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
        /// <param name="source">Source sequence to produce buffers over.</param>
        /// <param name="timeSpan">Maximum time length of a buffer.</param>
        /// <param name="count">Maximum element count of a buffer.</param>
        /// <param name="scheduler">Scheduler to run buffering timers on.</param>
        /// <returns>An observable sequence of buffers.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeSpan"/> is less than TimeSpan.Zero. -or- <paramref name="count"/> is less than or equal to zero.</exception>
        /// <remarks>

View on GitHub (pinned to 94b5d5ab91)