dotnet/reactive · error · ArgumentNullException

Thrown when condition is null (ArgumentNullException, param…

Error message

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

What it means

Generate builds an infinite/looping sequence driven by a condition predicate. Because condition is invoked during iteration, a null predicate would fail obscurely inside the iterator; the library validates it eagerly and throws ArgumentNullException with param name 'condition'.

Solutions

  1. Supply a concrete predicate, e.g. state => state < 10.
  2. Coalesce to a sane default: condition ?? (_ => false) for a never-iterating generator.
  3. Fix whatever was supposed to construct/inject the predicate.

Example fix

// before
var seq = GeneratorEx.Generate(0, cond, x => x + 1, x => x); // cond is null
// after
var seq = GeneratorEx.Generate(0, cond ?? (_ => false), x => x + 1, x => x);
Defensive patterns

Strategy: validation

Validate before calling

if (condition is null) throw new ArgumentException("condition predicate is required");

Type guard

bool HasPredicate<TState>(Func<TState, bool>? c) => c is not null;

Try / catch

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

Prevention

When it happens

Trigger: Calling Generate(initialState, null, iterate, resultSelector) — e.g. passing a Func<TState,bool> variable or expression-built predicate that resolved to null.

Common situations: Condition supplied by configuration or reflection that was not registered; ternary/conditional code paths assigning the predicate only in one branch; refactorings replacing a lambda with a nullable method group.

Related errors


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

Appendix: source

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

namespace System.Linq
{
    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)