dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'subscribe')

Error message

Value cannot be null. (Parameter 'subscribe')

What it means

The AnonymousObservable constructor throws ArgumentNullException when the subscribe delegate (Func<IObserver<T>, IDisposable>) is null. AnonymousObservable is the internal base used by Observable.Create; the delegate is invoked on every Subscribe call, so a null delegate would crash at subscription time rather than construction — the constructor fails fast instead.

Solutions

  1. Pass a real subscribe function: Observable.Create<int>(observer => { ...; return Disposable.Empty; })
  2. Default to a no-op subscribe (return Disposable.Empty) when a handler is optional
  3. Null-check the delegate at the factory boundary before calling Create

Example fix

// before
var obs = Observable.Create<int>(subscribeFn); // subscribeFn is null
// after
var obs = Observable.Create<int>(subscribeFn ?? (_ => Disposable.Empty));
Defensive patterns

Strategy: type-guard

Validate before calling

// C#
if (subscribe is null)
    subscribe = _ => Disposable.Empty;
var obs = Observable.Create(subscribe);

Type guard

bool HasSubscribe<T>(Func<IObserver<T>, IDisposable>? f) => f is not null;

Try / catch

try { var obs = Observable.Create<int>(subscribeFn); }
catch (ArgumentNullException ex) when (ex.ParamName == "subscribe")
{
    obs = Observable.Empty<int>();
}

Prevention

When it happens

Trigger: Calling new AnonymousObservable<T>(null) directly, or Observable.Create<T>(null), or Observable.Create<T>(observer => null-action) where a variable holding the subscribe lambda is null.

Common situations: Factory methods that conditionally build the subscribe function and return null in a default branch; DI/config-driven observer pipelines where the handler was not registered; refactors that renamed a method leaving a null method group.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/AnonymousObservable.cs:24

namespace System.Reactive
{
    /// <summary>
    /// Class to create an <see cref="IObservable{T}"/> instance from a delegate-based implementation of the <see cref="IObservable{T}.Subscribe(IObserver{T})"/> method.
    /// </summary>
    /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
    public sealed class AnonymousObservable<T> : ObservableBase<T>
    {
        private readonly Func<IObserver<T>, IDisposable> _subscribe;

        /// <summary>
        /// Creates an observable sequence object from the specified subscription function.
        /// </summary>
        /// <param name="subscribe"><see cref="IObservable{T}.Subscribe(IObserver{T})"/> method implementation.</param>
        /// <exception cref="ArgumentNullException"><paramref name="subscribe"/> is <c>null</c>.</exception>
        public AnonymousObservable(Func<IObserver<T>, IDisposable> subscribe)
        {
            _subscribe = subscribe ?? throw new ArgumentNullException(nameof(subscribe));
        }

        /// <summary>
        /// Calls the subscription function that was supplied to the constructor.
        /// </summary>
        /// <param name="observer">Observer to send notifications to.</param>
        /// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns>
        protected override IDisposable SubscribeCore(IObserver<T> observer)
        {
            return _subscribe(observer) ?? Disposable.Empty;
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)