dotnet/reactive · error · ArgumentNullException

Thrown when iterate is null (ArgumentNullException, param…

Error message

Thrown when iterate is null (ArgumentNullException, param name: iterate)

What it means

Generate requires an iterate function to advance the generator state each loop iteration. A null iterate would make the loop unable to progress, so it is validated eagerly and ArgumentNullException with param name 'iterate' is thrown.

Solutions

  1. Provide a real state transition, e.g. state => state + 1.
  2. Coalesce: iterate ?? (s => s) if the state should remain constant (careful: causes infinite loop unless condition terminates).
  3. Fix the null-producing source of the delegate.

Example fix

// before
var seq = GeneratorEx.Generate(0, c, step, x => x); // step is null
// after
var seq = GeneratorEx.Generate(0, c, step ?? (s => s + 1), x => x);
Defensive patterns

Strategy: validation

Validate before calling

if (iterate is null) throw new ArgumentException("iterate function is required");

Type guard

bool HasStepper<TState>(Func<TState, TState>? f) => f is not null;

Try / catch

try { var seq = GeneratorEx.Generate(state, condition, iterate, selector); }
catch (ArgumentNullException ex) when (ex.ParamName == "iterate") { log.Error("state transition function missing"); }

Prevention

When it happens

Trigger: Calling Generate(initialState, condition, null, resultSelector), typically with a state-transition delegate that is null because it was never assigned or a lookup returned null.

Common situations: State-machine steps configured externally; passing a method group whose method is missing after an API rename; deserialized delegate fields left null.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs:26

{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Generates a sequence by mimicking a for loop.
        /// </summary>
        /// <typeparam name="TState">State type.</typeparam>
        /// <typeparam name="TResult">Result sequence element type.</typeparam>
        /// <param name="initialState">Initial state of the generator loop.</param>
        /// <param name="condition">Loop condition.</param>
        /// <param name="iterate">State update function to run after every iteration of the generator loop.</param>
        /// <param name="resultSelector">Result selector to compute resulting sequence elements.</param>
        /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>
        public static IEnumerable<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 GenerateCore(initialState, condition, iterate, resultSelector);
        }

        private static IEnumerable<TResult> GenerateCore<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
        {
            for (var i = initialState; condition(i); i = iterate(i))
            {
                yield return resultSelector(i);
            }
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)