dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'count')

Error message

Value cannot be null. (Parameter 'count')

What it means

Buffer<TSource>(source, count) throws ArgumentNullException(nameof(count)) when count <= 0. Note this is actually an argument-range problem: a non-positive buffer size is invalid because a batch must contain at least one element, but the library throws ArgumentNullException with the parameter name 'count'. The check runs at operator composition time, before any subscription.

Solutions

  1. Pass a positive count (>= 1); clamp or default before calling: count = Math.Max(1, configuredCount).
  2. Validate configuration feeding the batch size at startup and fail with a clear domain error if it is <= 0.
  3. Catch ArgumentNullException('count') and surface a message telling the user the batch size must be positive.

Example fix

// before
var batches = source.Buffer(batchSize); // batchSize could be 0 from config
// after
if (batchSize <= 0) batchSize = 1; // or throw a domain-specific error
var batches = source.Buffer(batchSize);
Defensive patterns

Strategy: validation

Validate before calling

if (count <= 0) throw new ArgumentException("Buffer count must be >= 1", nameof(count));

Type guard

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

Try / catch

try { var batches = source.Buffer(count); }
catch (ArgumentNullException ex) when (ex.ParamName == "count") { throw new InvalidOperationException("Batch size must be positive; check configuration", ex); }

Prevention

When it happens

Trigger: Calling source.Buffer(0), source.Buffer(-1), or Buffer with a count computed from configuration/user input that is 0 or negative (e.g. pageSize = 0 from an unset setting).

Common situations: Batch size read from app configuration or a query-string parameter defaulting to 0; integer arithmetic producing 0 (e.g. batchSize = total / 0-safe divisor); uninitialized int fields passed straight through.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Buffer.cs:20

// 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.Collections.Generic;
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<IList<TSource>> Buffer<TSource>(this IAsyncObservable<TSource> source, int count)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (count <= 0)
                throw new ArgumentNullException(nameof(count));

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

        public static IAsyncObservable<IList<TSource>> Buffer<TSource>(this IAsyncObservable<TSource> source, int count, int skip)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (count <= 0)
                throw new ArgumentNullException(nameof(count));
            if (skip <= 0)
                throw new ArgumentNullException(nameof(skip));

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

View on GitHub (pinned to 94b5d5ab91)