{"record":{"id":"525b715223797b09","repo":"dotnet/reactive","slug":"thrown-when-elsesource-is-null-argumentnullexception-param","errorCode":null,"errorMessage":"Thrown when elseSource is null (ArgumentNullException, param name: elseSource)","messagePattern":"Thrown when elseSource is null \\(ArgumentNullException, param name: elseSource\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs","lineNumber":26,"sourceCode":"{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Returns an enumerable sequence based on the evaluation result of the given condition.\n        /// </summary>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"condition\">Condition to evaluate.</param>\n        /// <param name=\"thenSource\">Sequence to return in case the condition evaluates true.</param>\n        /// <param name=\"elseSource\">Sequence to return in case the condition evaluates false.</param>\n        /// <returns>Either of the two input sequences based on the result of evaluating the condition.</returns>\n        public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<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 Defer(() => condition() ? thenSource : elseSource);\n        }\n\n        /// <summary>\n        /// Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty\n        /// sequence.\n        /// </summary>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"condition\">Condition to evaluate.</param>\n        /// <param name=\"thenSource\">Sequence to return in case the condition evaluates true.</param>\n        /// <returns>The given input sequence if the condition evaluates true; otherwise, an empty sequence.</returns>\n        public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (thenSource == null)\n                throw new ArgumentNullException(nameof(thenSource));","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs#L8-L44","documentation":"The three-argument If operator requires elseSource as the branch returned when the condition is false. A null elseSource throws ArgumentNullException with param name 'elseSource' at call time. Unlike LINQ's standard Where-style APIs, Ix.NET makes even the else branch mandatory — use the two-argument overload for an implicit empty else.","triggerScenarios":"Calling If(condition, thenSource, null) — commonly when the developer assumed the else branch was optional, or a fallback sequence variable was null.","commonSituations":"Migrating from the 2-arg If overload and passing null 'for empty'; default/fallback sequences not yet configured; optional configuration sections providing the else sequence.","solutions":["Use the two-argument overload If(condition, thenSource), which returns an empty sequence when false.","Pass Enumerable.Empty<TResult>() as the else branch.","Fix the provider that should supply the fallback sequence."],"exampleFix":"// before\nvar seq = GeneratorEx.If(cond, thenSrc, null);\n// after\nvar seq = GeneratorEx.If(cond, thenSrc); // empty sequence when cond is false","handlingStrategy":"validation","validationCode":"// The else branch is NOT optional in the 3-arg overload; if you want empty, use the 2-arg overload:\nif (elseSource is null) { var seq = GeneratorEx.If(condition, thenSource); }","typeGuard":"bool HasElseSource<TResult>(IEnumerable<TResult>? s) => s is not null;","tryCatchPattern":"try { var seq = GeneratorEx.If(condition, thenSource, elseSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"elseSource\") { log.Error(\"else branch missing; use the 2-arg overload for an empty else\"); }","preventionTips":["Remember Ix.NET If has two overloads: 2-arg means 'empty else', 3-arg requires both sequences.","Pass Enumerable.Empty<TResult>() explicitly when you want a no-op else branch.","Document nullability of branch sequences in pipeline-builder APIs."],"tags":["null-argument","linq","conditional"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}