dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

AsyncObservable.Contains<TSource>(source, element) throws ArgumentNullException naming 'source' when the observable sequence to search is null. The check runs eagerly so failure surfaces at the call site, not at subscription time.

Solutions

  1. Pass a non-null IAsyncObservable<TSource>
  2. Coalesce to AsyncObservable.Empty<TSource>() when the source may be absent
  3. Fix the expression producing the source and check for null before chaining

Example fix

// before
var has = source.Contains(target); // source is null
// after
var has = (source ?? AsyncObservable.Empty<int>()).Contains(target);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) source = AsyncObservable.Empty<TSource>();

Type guard

public static bool IsNotNullSource<T>(IAsyncObservable<T>? s) => s is not null;

Try / catch

try { var has = source.Contains(element); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* handle absent source, e.g. return false */ }

Prevention

When it happens

Trigger: Calling Contains(null, someElement), often via method-chain syntax where the receiver expression evaluated to null: nullableSource.Contains(x).

Common situations: LINQ-style pipelines over observables produced by nullable factory methods, caches, or dictionary lookups that returned null; refactors introducing nullable sources.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Contains.cs:14

// 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.Collections.Generic;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<bool> Contains<TSource>(this IAsyncObservable<TSource> source, TSource element)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return CreateAsyncObservable<bool>.From(
                source,
                element,
                static (source, element, observer) => source.SubscribeSafeAsync(AsyncObserver.Contains(observer, element)));
        }

        public static IAsyncObservable<bool> Contains<TSource>(this IAsyncObservable<TSource> source, TSource element, IEqualityComparer<TSource> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return CreateAsyncObservable<bool>.From(
                source,
                (element, comparer),
                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Contains(observer, state.element, state.comparer)));

View on GitHub (pinned to 94b5d5ab91)