dotnet/reactive · error · ArgumentNullException

nameof(source)

Error message

nameof(source)

What it means

The IEnumerable-to-IObservable conversion Subscribe extension throws ArgumentNullException when the enumerable source is null. This operator subscribes an IObserver to the enumerable; Rx validates both arguments eagerly in the public wrapper before delegating to the implementation.

Solutions

  1. Ensure the enumerable is non-null before converting; treat null as an empty sequence at the source.
  2. Use Enumerable.Empty<TSource>() as the fallback when there is no data.
  3. Have producers return empty collections instead of null (standard .NET collection guidelines).

Example fix

// before
IEnumerable<int> data = repo.Query(); // may be null
data.Subscribe(observer);
// after
var data = repo.Query() ?? Enumerable.Empty<int>();
data.Subscribe(observer);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) source = Enumerable.Empty<T>();
var d = source.Subscribe(observer);

Type guard

static bool IsValid<T>(IEnumerable<T>? e) => e is not null;

Try / catch

try { var d = source.Subscribe(observer); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { log.Error("Null enumerable passed to Subscribe"); }

Prevention

When it happens

Trigger: Calling enumerable.Subscribe(observer) where the IEnumerable<TSource> is null — e.g. a method returning null for 'no data', or an uninitialized collection field.

Common situations: Happens when bridging legacy/LINQ data sources into Rx pipelines: a repository or parser returns null instead of an empty sequence and the result is converted to an observable.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Conversions.cs:26

namespace System.Reactive.Linq
{
    public static partial class Observable
    {
        #region + Subscribe +

        /// <summary>
        /// Subscribes an observer to an enumerable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Enumerable sequence to subscribe to.</param>
        /// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param>
        /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception>
        public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return s_impl.Subscribe(source, observer);
        }

        /// <summary>
        /// Subscribes an observer to an enumerable sequence, using the specified scheduler to run the enumeration loop.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Enumerable sequence to subscribe to.</param>
        /// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param>
        /// <param name="scheduler">Scheduler to perform the enumeration on.</param>
        /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>

View on GitHub (pinned to 94b5d5ab91)