dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(iterate));

Error message

throw new ArgumentNullException(nameof(iterate));

What it means

Observable.Generate (the TimeSpan timeSelector overload) throws ArgumentNullException because the iterate delegate parameter was null. Rx validates all delegate arguments eagerly at subscription-entry point so failures surface at call time instead of inside the running sequence. Pass a non-null state-transition function.

Solutions

  1. Pass a non-null Func<TState, TState> as the iterate argument (e.g. state => state + 1).
  2. Check argument order: Generate takes (initialState, condition, iterate, resultSelector, timeSelector[, scheduler]).
  3. If iterate may be absent by design, supply an identity function state => state instead of null.

Example fix

// before
var xs = Observable.Generate(0, i => i < 10, null, i => i, i => TimeSpan.FromSeconds(1));
// after
var xs = Observable.Generate(0, i => i < 10, i => i + 1, i => i, i => TimeSpan.FromSeconds(1));
Defensive patterns

Strategy: validation

Validate before calling

if (iterate is null) throw new InvalidOperationException("iterate delegate must be provided before calling Observable.Generate");

Type guard

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

Try / catch

try { var xs = Observable.Generate(0, cond, iterate, sel, timeSel); }
catch (ArgumentNullException ex) when (ex.ParamName == "iterate") { /* substitute identity or rethrow with context */ }

Prevention

When it happens

Trigger: Calling Observable.Generate(initialState, condition, iterate: null, resultSelector, timeSelector) where timeSelector is Func<TState, TimeSpan> — e.g. a mistyped or uninitialized Func<TState,TState> variable passed as the third delegate.

Common situations: Building polling/loop sequences with delays where the state-advance lambda is built conditionally or loaded from config and ends up null; refactors that swap argument order and shift a lambda into the wrong slot leaving iterate empty.

Related errors


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

Appendix: source

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

        /// <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>
        /// <param name="timeSelector">Time selector function to control the speed of values being produced each iteration.</param>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> or <paramref name="timeSelector"/> 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, TimeSpan> timeSelector)
        {
            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));
            }

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

        /// <summary>
        /// Generates an observable sequence by running a state-driven and temporal loop producing the sequence's elements, using the specified scheduler to run timers and to send out observer messages.
        /// </summary>

View on GitHub (pinned to 94b5d5ab91)