dotnet/reactive · error · ArgumentNullException

thenSource

Error message

thenSource

What it means

AsyncObservable.If throws ArgumentNullException when thenSource is null. The thenSource is the observable executed when the condition evaluates true, and the operator requires both branches to be valid observables up front. This sync-condition overload validates before constructing the returned observable, so the exception throws at operator-creation time, not subscription time.

Solutions

  1. Pass a valid IAsyncObservable<TResult> for thenSource.
  2. If the true-branch may be absent, substitute Empty<TResult>() instead of null.
  3. Check the source factory/registry so it returns a non-null observable or throws a descriptive error itself.

Example fix

// before
var obs = AsyncObservable.If(cond, null, elseSource);
// after
var obs = AsyncObservable.If(cond, thenSource ?? AsyncObservable.Empty<Result>(), elseSource);
Defensive patterns

Strategy: validation

Validate before calling

if (thenSource == null) throw new ArgumentNullException(nameof(thenSource));
// or coerce: thenSource ??= AsyncObservable.Empty<TResult>();

Type guard

bool HasThenSource<T>(IAsyncObservable<T> o) => o is not null;

Try / catch

try { var obs = AsyncObservable.If(condition, thenSource, elseSource); }
catch (ArgumentNullException ex) when (ex.ParamName == "thenSource") { /* fall back to Empty and log */ }

Prevention

When it happens

Trigger: Calling AsyncObservable.If(condition, null, elseSource); typical when thenSource comes from a factory/registry lookup that returned null, or when a conditional expression `cond ? then : else` had a null branch.

Common situations: Building observable pipelines from config where a named source is missing; refactors renaming a source variable; DI containers returning null for unregistered observables.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs:21

// See the LICENSE file in the project root for more information. 

using System.Reactive.Concurrency;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource) => If(condition, thenSource, Empty<TResult>());

        public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource, IAsyncScheduler scheduler) => If(condition, thenSource, Empty<TResult>(scheduler));

        public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource, IAsyncObservable<TResult> elseSource)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (thenSource == null)
                throw new ArgumentNullException(nameof(thenSource));
            if (elseSource == null)
                throw new ArgumentNullException(nameof(elseSource));

            return CreateAsyncObservable<TResult>.From(
                thenSource,
                (elseSource, condition),
                static (thenSource, state, observer) =>
                {
                    var b = default(bool);

                    try
                    {
                        b = state.condition();
                    }
                    catch (Exception ex)
                    {
                        return Throw<TResult>(ex).SubscribeAsync(observer);
                    }

View on GitHub (pinned to 94b5d5ab91)