{"record":{"id":"3d286a90f1c8b50d","repo":"dotnet/reactive","slug":"thensource","errorCode":null,"errorMessage":"thenSource","messagePattern":"thenSource","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs","lineNumber":21,"sourceCode":"// See the LICENSE file in the project root for more information. \n\nusing 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                    }","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs#L3-L39","documentation":"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.","triggerScenarios":"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.","commonSituations":"Building observable pipelines from config where a named source is missing; refactors renaming a source variable; DI containers returning null for unregistered observables.","solutions":["Pass a valid IAsyncObservable<TResult> for thenSource.","If the true-branch may be absent, substitute Empty<TResult>() instead of null.","Check the source factory/registry so it returns a non-null observable or throws a descriptive error itself."],"exampleFix":"// before\nvar obs = AsyncObservable.If(cond, null, elseSource);\n// after\nvar obs = AsyncObservable.If(cond, thenSource ?? AsyncObservable.Empty<Result>(), elseSource);","handlingStrategy":"validation","validationCode":"if (thenSource == null) throw new ArgumentNullException(nameof(thenSource));\n// or coerce: thenSource ??= AsyncObservable.Empty<TResult>();","typeGuard":"bool HasThenSource<T>(IAsyncObservable<T> o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObservable.If(condition, thenSource, elseSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"thenSource\") { /* fall back to Empty and log */ }","preventionTips":["Never let source factories/registries return null — throw or return Empty","Null-check observables resolved from DI before building pipelines","Use the null-coalescing operator at pipeline construction sites"],"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"}