{"record":{"id":"e59779f868ebc1d9","repo":"dotnet/reactive","slug":"elsesource","errorCode":null,"errorMessage":"elseSource","messagePattern":"elseSource","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs","lineNumber":23,"sourceCode":"using System.Reactive.Concurrency;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource) => If(condition, thenSource, Empty<TResult>());\n\n        public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource, IAsyncScheduler scheduler) => If(condition, thenSource, Empty<TResult>(scheduler));\n\n        public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource, IAsyncObservable<TResult> elseSource)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (thenSource == null)\n                throw new ArgumentNullException(nameof(thenSource));\n            if (elseSource == null)\n                throw new ArgumentNullException(nameof(elseSource));\n\n            return CreateAsyncObservable<TResult>.From(\n                thenSource,\n                (elseSource, condition),\n                static (thenSource, state, observer) =>\n                {\n                    var b = default(bool);\n\n                    try\n                    {\n                        b = state.condition();\n                    }\n                    catch (Exception ex)\n                    {\n                        return Throw<TResult>(ex).SubscribeAsync(observer);\n                    }\n\n                    return (b ? thenSource : state.elseSource).SubscribeSafeAsync(observer);","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs#L5-L41","documentation":"AsyncObservable.If throws ArgumentNullException when elseSource is null. The elseSource runs when the condition evaluates false and must be a concrete observable; the guard ensures both branches exist at construction time. Thrown synchronously from the operator factory, so it points directly at the faulty call.","triggerScenarios":"Calling AsyncObservable.If(condition, thenSource, null) — often from conditional assembly where the else branch is optional, or a two-argument helper mistakenly calling the three-argument overload with a trailing null.","commonSituations":"Optional fallback semantics implemented as null instead of Empty; config-driven pipelines missing an else source; overload confusion between the scheduler overload (which fills else with Empty) and the two-source overload.","solutions":["Supply a non-null elseSource observable.","Use the scheduler overload If(condition, thenSource, scheduler) if you want an implicit Empty else branch.","Substitute `elseSource ?? AsyncObservable.Empty<TResult>()` when the else branch is genuinely optional."],"exampleFix":"// before\nvar obs = AsyncObservable.If(cond, thenSource, null);\n// after\nvar obs = AsyncObservable.If(cond, thenSource, AsyncObservable.Empty<Result>());","handlingStrategy":"validation","validationCode":"if (elseSource == null) throw new ArgumentNullException(nameof(elseSource));\n// or coerce: elseSource ??= AsyncObservable.Empty<TResult>();","typeGuard":"bool HasElseSource<T>(IAsyncObservable<T> o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObservable.If(condition, thenSource, elseSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"elseSource\") { /* fall back to Empty and log */ }","preventionTips":["Model an absent else branch as Empty<TResult>(), not null","Prefer the If(condition, thenSource, scheduler) overload when no else is needed","Standardize a pipeline-builder helper that coerces null branches"],"tags":["argument-null","conditional-operator","rx-operator"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}