dotnet/reactive · error · ArgumentNullException

nameof(subscribe)

Error message

nameof(subscribe)

What it means

Observable.Create<TResult> with a Func<IObserver<TResult>, IDisposable> throws ArgumentNullException when the subscribe delegate is null. Create is the canonical way to build custom observables, and Rx validates the delegate before constructing the observable. The null check ensures the failure occurs at Create, not at subscription time.

Solutions

  1. Supply a valid subscribe lambda: o => { ...; return Disposable.Empty; }
  2. Fix the factory/variable that resolved to null
  3. Guard with a null check before Create

Example fix

// before
var obs = Observable.Create<int>(subscribe); // subscribe is null
// after
var obs = Observable.Create<int>(o =>
{
    o.OnNext(1);
    o.OnCompleted();
    return Disposable.Empty;
});
Defensive patterns

Strategy: validation

Validate before calling

if (subscribe == null) throw new ArgumentNullException(nameof(subscribe));
var obs = Observable.Create<T>(subscribe);

Type guard

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

Try / catch

try { var obs = Observable.Create<T>(subscribe); }
catch (ArgumentNullException ex) when (ex.ParamName == "subscribe") { /* handle null delegate */ }

Prevention

When it happens

Trigger: Calling Observable.Create<TResult>(subscribe) with a null Func<IObserver<TResult>, IDisposable>, e.g. a delegate variable not assigned or a factory returning null.

Common situations: Conditional delegate assignment patterns, refactoring that removed the subscription lambda, or reflection/late-bound delegate resolution failing.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:31

        #region + Create +

        /// <summary>
        /// Creates an observable sequence from a specified Subscribe method implementation.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
        /// <param name="subscribe">Implementation of the resulting observable sequence's Subscribe method.</param>
        /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="subscribe"/> is null.</exception>
        /// <remarks>
        /// Use of this operator is preferred over manual implementation of the <see cref="IObservable{T}"/> interface. In case
        /// you need a type implementing <see cref="IObservable{T}"/> rather than an anonymous implementation, consider using
        /// the <see cref="ObservableBase{T}"/> abstract base class.
        /// </remarks>
        public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, IDisposable> subscribe)
        {
            if (subscribe == null)
            {
                throw new ArgumentNullException(nameof(subscribe));
            }

            return s_impl.Create(subscribe);
        }

        /// <summary>
        /// Creates an observable sequence from a specified Subscribe method implementation.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
        /// <param name="subscribe">Implementation of the resulting observable sequence's Subscribe method, returning an Action delegate that will be wrapped in an IDisposable.</param>
        /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="subscribe"/> is null.</exception>
        /// <remarks>
        /// Use of this operator is preferred over manual implementation of the <see cref="IObservable{T}"/> interface. In case
        /// you need a type implementing <see cref="IObservable{T}"/> rather than an anonymous implementation, consider using
        /// the <see cref="ObservableBase{T}"/> abstract base class.
        /// </remarks>
        public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Action> subscribe)

View on GitHub (pinned to 94b5d5ab91)