dotnet/reactive · error · ArgumentNullException

nameof(resultSelector)

Error message

nameof(resultSelector)

What it means

AsyncObservable.Generate throws ArgumentNullException with parameter name 'resultSelector' when the Func<TState,TResult> mapping argument is null. The result selector converts each state into an emitted item, so a null selector is invalid.

Solutions

  1. Supply a non-null selector, e.g. x => x * 2, as resultSelector
  2. Verify the parameter order (state, condition, iterate, resultSelector [, scheduler])
  3. Guard the selector at its creation site with ?? throw new ArgumentNullException

Example fix

// before
AsyncObservable.Generate(0, x => x < 10, x => x + 1, null);
// after
AsyncObservable.Generate(0, x => x < 10, x => x + 1, x => x);
Defensive patterns

Strategy: validation

Validate before calling

if (resultSelector == null)
    throw new ArgumentNullException(nameof(resultSelector), "Generate requires a result selector");
var obs = AsyncObservable.Generate(state, condition, iterate, resultSelector);

Type guard

static bool HasSelector<TState, TResult>(Func<TState, TResult>? selector) => selector != null;

Try / catch

try
{
    var obs = AsyncObservable.Generate(state, condition, iterate, selector);
}
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector")
{
    // correct the argument order or supply a selector
}

Prevention

When it happens

Trigger: Calling AsyncObservable.Generate(initialState, condition, iterate, null) or the scheduler overload with a null fourth argument — often from argument-order mistakes or a selector variable that was never initialized.

Common situations: Passing arguments in the wrong order so a null lands in the selector slot; selectors built via reflection/Expression that compile to null; nullable delegate fields not yet assigned.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs:19

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Reactive.Concurrency;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<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 Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));
        }

        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)
        {
            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 (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector, scheduler));
        }

View on GitHub (pinned to 94b5d5ab91)