dotnet/reactive · error · ArgumentNullException

nameof(condition)

Error message

nameof(condition)

What it means

Observable.Generate throws ArgumentNullException when the 'condition' delegate passed to it is null. Rx requires all three delegates (condition, iterate, resultSelector) to be non-null because the generator loop calls them on every step; a null delegate would cause a NullReferenceException deep inside the operator instead of a clear argument check at the call site.

Solutions

  1. Pass a non-null condition predicate, e.g. x => x < 10
  2. If the condition is optional, substitute a permissive default like _ => true before calling Generate
  3. Check the variable being passed for null before invoking Generate

Example fix

// before
Observable.Generate(0, maybeCondition, i => i + 1, i => i)
// after
var cond = maybeCondition ?? (_ => true);
Observable.Generate(0, cond, i => i + 1, i => i)
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null) throw new ArgumentNullException(nameof(condition)); // or guard before calling

Type guard

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

Try / catch

try { Observable.Generate(state, cond, it, sel); } catch (ArgumentNullException ex) when (ex.ParamName == "condition") { /* supply default predicate */ }

Prevention

When it happens

Trigger: Calling System.Reactive.Linq.Observable.Generate<TState, TResult>(initialState, null, iterate, resultSelector) — e.g. a condition lambda variable that was never assigned, or a conditional expression that resolved to null.

Common situations: Building a generator from config-driven or dynamically composed delegates where an optional predicate was not supplied; refactoring that removed a lambda but left a null variable; DI factories returning null delegates.

Related errors


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

Appendix: source

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

        #region + Generate +

        /// <summary>
        /// Generates an observable sequence by running a state-driven loop producing the sequence's elements.
        /// </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>

View on GitHub (pinned to 94b5d5ab91)