dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

AsyncObserver.ToHashSet(observer) wraps a downstream observer to accumulate elements into a HashSet. The downstream observer must be non-null; passing null throws ArgumentNullException(Parameter 'observer') synchronously. This guard protects the operator's internal ObserverBase wiring from a null downstream sink.

Solutions

  1. Pass a valid IAsyncObserver<HashSet<TSource>> instance to AsyncObserver.ToHashSet.
  2. Validate the observer argument in your own operator before forwarding it.
  3. Ensure the observer comes from the Create callback's observer parameter, not a separately managed null field.

Example fix

// before
var obs = AsyncObserver.ToHashSet<int>(observer); // observer null
// after
if (observer == null) throw new InvalidOperationException("observer not wired");
var obs = AsyncObserver.ToHashSet<int>(observer);
Defensive patterns

Strategy: validation

Validate before calling

if (observer == null) throw new ArgumentNullException(nameof(observer));

Type guard

bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;

Try / catch

try { var obs = AsyncObserver.ToHashSet(observer); } catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* fix observer wiring */ }

Prevention

When it happens

Trigger: Calling AsyncObserver.ToHashSet(null), typically when building custom operator plumbing where the observer argument came from an uninitialized variable or an earlier Create call that passed null.

Common situations: Writing custom AsyncObservable operators with Create<TSource,TResult> and accidentally forwarding a null observer; unit-test scaffolding that forgot to construct a test observer.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToHashSet.cs:38

        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return CreateAsyncObservable<HashSet<TSource>>.From(
                source,
                comparer,
                static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.ToHashSet(observer, comparer)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> ToHashSet<TSource>(IAsyncObserver<HashSet<TSource>> observer)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));

            return ToHashSet(observer, EqualityComparer<TSource>.Default);
        }

        public static IAsyncObserver<TSource> ToHashSet<TSource>(IAsyncObserver<HashSet<TSource>> observer, IEqualityComparer<TSource> comparer)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return Aggregate<TSource, HashSet<TSource>>(observer, new HashSet<TSource>(comparer), (set, x) => { set.Add(x); return set; });
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)