dotnet/reactive · error · ArgumentNullException

Argument cannot be null (Parameter name: resultSelector)

Error message

Argument cannot be null (Parameter name: resultSelector)

What it means

For(IEnumerable<TSource>, Func<TSource,IAsyncObservable<TResult>>) throws ArgumentNullException because the resultSelector delegate is null. For each element of the source, resultSelector must produce an IAsyncObservable<TResult>; the operator validates it at creation time and names 'resultSelector' in the exception. A null selector cannot fall back to any default behavior.

Solutions

  1. Pass a valid selector, e.g. x => AsyncObservable.Return(Transform(x)).
  2. If the selector is optional, substitute a default: resultSelector ?? (x => AsyncObservable.Return(x)).
  3. Fix the factory/config path so it always yields a delegate or short-circuit to Empty when absent.
  4. Validate the selector at the API boundary before composing the pipeline.

Example fix

// before
Func<string, IAsyncObservable<int>> selector = config.Selector; // null
var result = AsyncObservable.For(names, selector);

// after
var result = AsyncObservable.For(names, config.Selector ?? (x => AsyncObservable.Return(x.Length)));
Defensive patterns

Strategy: validation

Validate before calling

if (resultSelector == null)
    throw new InvalidOperationException("For requires a result selector; provide one or use Empty.");
var result = AsyncObservable.For(source, resultSelector);

Type guard

static bool IsValidSelector<T, R>(Func<T, IAsyncObservable<R>> s) => s is not null;

Try / catch

try { var result = AsyncObservable.For(source, selector); }
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* supply default selector or fail with config error */ }

Prevention

When it happens

Trigger: Calling AsyncObservable.For(source, (Func<TSource,IAsyncObservable<TResult>>)null) — usually a null local, a conditional delegate assignment that skipped assignment, or forwarding an optional selector parameter left null.

Common situations: Factories that build the selector from configuration and return null when config is missing; refactoring that renamed a method and the method-group reference was lost; optional parameters defaulting to null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/For.cs:21

// See the LICENSE file in the project root for more information. 

using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        // REVIEW: Use a tail-recursive sink.
        // TODO: Add IAsyncEnumerable<T> based overlaod.

        public static IAsyncObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IAsyncObservable<TResult>> resultSelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));

            return For(source, x => new ValueTask<IAsyncObservable<TResult>>(resultSelector(x)));
        }

        public static IAsyncObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, ValueTask<IAsyncObservable<TResult>>> resultSelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));

            return Create<TResult>(async observer =>
            {
                var subscription = new SerialAsyncDisposable();

                var enumerator = source.GetEnumerator();

                var o = default(IAsyncObserver<TResult>);

View on GitHub (pinned to 94b5d5ab91)