{"record":{"id":"72599c55e1c93db0","repo":"dotnet/reactive","slug":"thrown-when-thensource-is-null-argumentnullexception-param","errorCode":null,"errorMessage":"Thrown when thenSource is null (ArgumentNullException, param name: thenSource)","messagePattern":"Thrown when thenSource is null \\(ArgumentNullException, param name: thenSource\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs","lineNumber":24,"sourceCode":"\nnamespace System.Linq\n{\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));","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs#L6-L42","documentation":"The three-argument If operator requires thenSource as the branch returned when the condition is true. It is validated eagerly; a null thenSource throws ArgumentNullException with param name 'thenSource'.","triggerScenarios":"Calling If(condition, null, elseSource) — e.g. the 'true' branch sequence is null because a query method, cache lookup, or configuration-provided sequence was absent.","commonSituations":"Branch sequences composed from optional data sources; dictionary of named sequences where the key lookup missed; refactors replacing a default sequence with a nullable variable.","solutions":["Pass an actual sequence for the true branch, or use the two-argument overload and supply Enumerable.Empty<TResult>() explicitly.","Coalesce: thenSource ?? Enumerable.Empty<TResult>().","Fix the source that should provide the branch sequence."],"exampleFix":"// before\nvar seq = GeneratorEx.If(cond, thenSrc, elseSrc); // thenSrc is null\n// after\nvar seq = GeneratorEx.If(cond, thenSrc ?? Enumerable.Empty<int>(), elseSrc);","handlingStrategy":"validation","validationCode":"if (thenSource is null) thenSource = Enumerable.Empty<TResult>();","typeGuard":"bool HasThenSource<TResult>(IEnumerable<TResult>? s) => s is not null;","tryCatchPattern":"try { var seq = GeneratorEx.If(condition, thenSource, elseSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"thenSource\") { log.Error(\"true-branch sequence missing\"); }","preventionTips":["Coalesce branch sequences with ?? Enumerable.Empty<TResult>().","Ensure branch-producing methods return empty rather than null.","Validate all branch sources when building conditional pipelines."],"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"}