{"record":{"id":"b58290c7d455b8be","repo":"dotnet/reactive","slug":"thrown-when-condition-is-null-argumentnullexception-param","errorCode":null,"errorMessage":"Thrown when condition is null (ArgumentNullException, param name: condition)","messagePattern":"Thrown when condition is null \\(ArgumentNullException, param name: condition\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/DoWhile.cs","lineNumber":23,"sourceCode":"using System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.\n        /// </summary>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence to repeat while the condition evaluates true.</param>\n        /// <param name=\"condition\">Loop condition.</param>\n        /// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>\n        public static IEnumerable<TResult> DoWhile<TResult>(this IEnumerable<TResult> source, Func<bool> condition)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n\n            return source.Concat(While(condition, source));\n        }\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":29,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/DoWhile.cs#L5-L29","documentation":"DoWhile repeats the source sequence as long as the postcondition delegate holds; a null condition would make the repeat decision undefined, so the operator throws ArgumentNullException for parameter 'condition'. Fix: pass a non-null Func<bool>.","triggerScenarios":"Calling DoWhile(source, null) — e.g. the condition Func<bool> came from an optional configuration or a nullable delegate field.","commonSituations":"A delegate stored in a nullable field/property not yet initialized; a factory method returning null for the condition; passing a method group on a null object (obj?.Condition compiles to null).","solutions":["Supply a valid Func<bool> — use () => false if 'no repeat' is intended.","Verify the delegate source (field/factory) is initialized before the call.","Add a null guard or default at the point where the condition is obtained."],"exampleFix":"// before\nFunc<bool> cond = GetCondition(); // may return null\nvar result = source.DoWhile(cond);\n// after\nvar result = source.DoWhile(cond ?? (() => false));","handlingStrategy":"validation","validationCode":"if (condition is null) throw new ArgumentNullException(nameof(condition));\nDoWhile(source, condition ?? (() => false));","typeGuard":"bool HasCondition(Func<bool> c) => c is not null;","tryCatchPattern":"try { var q = source.DoWhile(condition); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"condition\") { /* use default condition */ }","preventionTips":["Initialize delegate fields at declaration, not lazily.","Avoid obj?.Method passing which silently yields null.","Provide defaults (() => false) for optional conditions.","Use nullable reference types to catch uninitialized delegates."],"tags":["null-argument","argument-validation","delegate"],"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"}