{"record":{"id":"fd22fe5690c9df99","repo":"dotnet/reactive","slug":"condition","errorCode":null,"errorMessage":"condition","messagePattern":"condition","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs","lineNumber":19,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// 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                    {","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/If.cs#L1-L37","documentation":"AsyncObservable.If (Func<bool> overload) throws ArgumentNullException when the condition delegate is null. The condition decides at subscription time whether to run thenSource or elseSource; a null condition makes the operator's decision impossible, so it fails fast. Three sibling overloads (sync condition, async Func<ValueTask<bool>> condition, scheduler variants) enforce the same guard.","triggerScenarios":"Calling AsyncObservable.If(null, thenSource, elseSource) or If(null, thenSource, scheduler); commonly from a boolean-returning delegate stored in a nullable field or built from parsed configuration.","commonSituations":"Rule engines where predicates come from config/DI and may be unset; refactors replacing Func<bool> with Func<ValueTask<bool>> leaving the old null; template code with placeholder nulls.","solutions":["Pass a non-null Func<bool>, e.g. `() => featureEnabled`.","Check you are calling the intended overload — a null where a scheduler or source is expected can shift into the condition slot.","If the condition is optional by design, default it to `() => true` or `() => false` before calling."],"exampleFix":"// before\nvar obs = AsyncObservable.If(null, thenSource, elseSource);\n// after\nvar obs = AsyncObservable.If(() => settings.Enabled, thenSource, elseSource);","handlingStrategy":"validation","validationCode":"if (condition == null) throw new ArgumentNullException(nameof(condition));\n// or default: condition ??= () => true;","typeGuard":"bool HasCondition(Func<bool> f) => f is not null;","tryCatchPattern":"try { var obs = AsyncObservable.If(condition, thenSource, elseSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"condition\") { /* substitute default predicate and retry */ }","preventionTips":["Default conditional predicates at configuration load time","Use named arguments to avoid overload/positional confusion","Never store Func<bool> rules as null in rule registries"],"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"}