dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(timeSelector));

Error message

throw new ArgumentNullException(nameof(timeSelector));

What it means

Observable.Generate (the TimeSpan overload, no scheduler) throws ArgumentNullException because the timeSelector delegate was null. The timeSelector decides the delay before each element, so Rx requires it non-null at call time. Provide a Func<TState, TimeSpan> returning the per-element delay.

Solutions

  1. Pass a non-null Func<TState, TimeSpan> as timeSelector (e.g. _ => TimeSpan.FromSeconds(1)).
  2. Use the non-timed Observable.Generate overload if no per-element delay is wanted.
  3. Check that the config-derived delay function is initialized before the call.

Example fix

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

Strategy: validation

Validate before calling

if (timeSelector is null) throw new InvalidOperationException("timeSelector must be provided for the timed Generate overload");

Type guard

static bool HasTimeSelector<TState>(Func<TState, TimeSpan> timeSelector) => timeSelector is not null;

Try / catch

try { var xs = Observable.Generate(0, cond, iter, sel, timeSelector); }
catch (ArgumentNullException ex) when (ex.ParamName == "timeSelector") { /* use non-timed Generate instead */ }

Prevention

When it happens

Trigger: Calling Observable.Generate(initialState, condition, iterate, resultSelector, timeSelector: null) — e.g. omitting the timing lambda when converting a plain Generate call into a timed one.

Common situations: Migrating from the non-timed Observable.Generate overload to the timed one and forgetting the extra delegate; building the timing function from configuration that was not loaded.

Related errors


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

Appendix: source

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

        {
            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>
        /// <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>
        /// <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>

View on GitHub (pinned to 94b5d5ab91)