dotnet/reactive · error · ArgumentNullException

iterate

Error message

iterate

What it means

The scheduler-based timed Observable.Generate overload throws ArgumentNullException when the iterate function (Func<TState, TState>) is null. iterate advances the state between elements; without it Rx cannot progress the generation loop. The check runs eagerly before the observable is returned.

Solutions

  1. Provide a state transition function, e.g. i => i + 1.
  2. If no state change is desired, use state => state as an explicit no-op iterator.
  3. Validate all delegate parameters together at your wrapper's entry point so the failure is attributed to the right argument.

Example fix

// before
var xs = Observable.Generate(0, i => i < 3, null, i => i, i => TimeSpan.Zero, Scheduler.Default);
// after
var xs = Observable.Generate(0, i => i < 3, i => i + 1, i => i, i => TimeSpan.Zero, Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (iterate == null)
    throw new ArgumentNullException(nameof(iterate));
var xs = Observable.Generate(initialState, condition, iterate, resultSelector, timeSelector, scheduler);

Type guard

bool HasIterator<TState>(Func<TState, TState> iterate) => iterate != null;

Try / catch

try
{
    var xs = Observable.Generate(state, cond, iter, sel, timeSel, scheduler);
}
catch (ArgumentNullException ex) when (ex.ParamName == "iterate")
{
    logger.LogError(ex, "Generate iterate function was null");
}

Prevention

When it happens

Trigger: Calling Observable.Generate<TState, TResult>(initialState, condition, iterate, resultSelector, timeSelector, scheduler) with iterate == null, typically when the state-advancing function is conditionally constructed or accidentally omitted in a positional-argument call with many lambdas.

Common situations: Long argument lists of five delegates where one slot is skipped; dynamic query builders that fill lambdas from a dictionary of steps where the 'iterate' entry is missing; code generation or DSL output producing null.

Related errors


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

Appendix: source

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

        /// <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>
        /// <param name="timeSelector">Time selector function to control the speed of values being produced each iteration.</param>
        /// <param name="scheduler">Scheduler on which to run the generator loop.</param>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> or <paramref name="timeSelector"/> or <paramref name="scheduler"/> is null.</exception>
        public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, Func<TState, DateTimeOffset> timeSelector, IScheduler 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 (timeSelector == null)
            {
                throw new ArgumentNullException(nameof(timeSelector));
            }

            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            return s_impl.Generate(initialState, condition, iterate, resultSelector, timeSelector, scheduler);

View on GitHub (pinned to 94b5d5ab91)