dotnet/reactive · error · ArgumentNullException

nameof(iterate)

Error message

nameof(iterate)

What it means

AsyncObservable.Generate requires an iterate function to advance the state and throws ArgumentNullException (parameter 'iterate') when it is null. Without an iterator the generated sequence could never progress, so the library rejects it eagerly.

Solutions

  1. Pass a non-null step function such as x => x + 1 for iterate
  2. Check argument order: Generate(initialState, condition, iterate, resultSelector)
  3. Add a null-check or default value where the iterate delegate is produced

Example fix

// before
AsyncObservable.Generate(0, x => x < 10, null, x => x);
// after
AsyncObservable.Generate(0, x => x < 10, x => x + 1, x => x);
Defensive patterns

Strategy: validation

Validate before calling

if (iterate == null)
    throw new ArgumentNullException(nameof(iterate), "Generate requires an iterate function");
var obs = AsyncObservable.Generate(state, condition, iterate, resultSelector);

Type guard

static bool HasIterate<TState>(Func<TState, TState>? iterate) => iterate != null;

Try / catch

try
{
    var obs = AsyncObservable.Generate(state, condition, iterate, selector);
}
catch (ArgumentNullException ex) when (ex.ParamName == "iterate")
{
    // supply a default step function or report config error
}

Prevention

When it happens

Trigger: Calling AsyncObservable.Generate(initialState, condition, null, resultSelector) — the Func<TState,TState> step function is null, typically from an uninitialized member or a misordered argument list.

Common situations: Swapping the iterate and resultSelector arguments accidentally; a state-advance function resolved to null from configuration; incomplete refactoring leaving the lambda unassigned.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs:17

// 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.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (iterate == null)
                throw new ArgumentNullException(nameof(iterate));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));

            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));
        }

        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (iterate == null)
                throw new ArgumentNullException(nameof(iterate));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector, scheduler));

View on GitHub (pinned to 94b5d5ab91)