dotnet/reactive · error · ArgumentNullException

nameof(iterate)

Error message

nameof(iterate)

What it means

Observable.Generate throws ArgumentNullException when the 'iterate' (state-advancing) delegate is null. The operator must invoke iterate on every step to move from one state to the next, so Rx validates it up front and throws a descriptive ArgumentNullException rather than failing later.

Solutions

  1. Pass a non-null iterate function, e.g. i => i + 1
  2. Default a missing step function to a terminator such as _ => initialState to stop the loop
  3. Validate the delegate before calling Generate

Example fix

// before
Observable.Generate(0, c, null, r)
// after
Observable.Generate(0, c, i => i + 1, r)
Defensive patterns

Strategy: validation

Validate before calling

if (iterate == null) throw new ArgumentNullException(nameof(iterate));

Type guard

bool HasIterate(Func<TState,TState> it) => it is not null;

Try / catch

try { Observable.Generate(state, cond, it, sel); } catch (ArgumentNullException ex) when (ex.ParamName == "iterate") { /* fix step function */ }

Prevention

When it happens

Trigger: Calling Observable.Generate<TState, TResult>(initialState, condition, null, resultSelector), e.g. a step function variable that is null or a lambda assigned to a null-able delegate field.

Common situations: Dynamically composed step functions where a factory returned null; typos assigning the lambda to the wrong variable; optional plugin hooks not wired.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:366

        /// </summary>
        /// <typeparam name="TState">The type of the state used in the generator loop.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
        /// <param name="initialState">Initial state.</param>
        /// <param name="condition">Condition to terminate generation (upon returning false).</param>
        /// <param name="iterate">Iteration step function.</param>
        /// <param name="resultSelector">Selector function for results produced in the sequence.</param>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> is null.</exception>
        public static IObservable<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 s_impl.Generate(initialState, condition, iterate, resultSelector);
        }

        /// <summary>
        /// Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.
        /// </summary>
        /// <typeparam name="TState">The type of the state used in the generator loop.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
        /// <param name="initialState">Initial state.</param>
        /// <param name="condition">Condition to terminate generation (upon returning false).</param>
        /// <param name="iterate">Iteration step function.</param>

View on GitHub (pinned to 94b5d5ab91)