{"record":{"id":"653beed7f90314fe","repo":"dotnet/reactive","slug":"thrown-when-iterate-is-null-argumentnullexception-param-name","errorCode":null,"errorMessage":"Thrown when iterate is null (ArgumentNullException, param name: iterate)","messagePattern":"Thrown when iterate is null \\(ArgumentNullException, param name: iterate\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs","lineNumber":26,"sourceCode":"{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Generates a sequence by mimicking a for loop.\n        /// </summary>\n        /// <typeparam name=\"TState\">State type.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"initialState\">Initial state of the generator loop.</param>\n        /// <param name=\"condition\">Loop condition.</param>\n        /// <param name=\"iterate\">State update function to run after every iteration of the generator loop.</param>\n        /// <param name=\"resultSelector\">Result selector to compute resulting sequence elements.</param>\n        /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>\n        public static IEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (iterate == null)\n                throw new ArgumentNullException(nameof(iterate));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return GenerateCore(initialState, condition, iterate, resultSelector);\n        }\n\n        private static IEnumerable<TResult> GenerateCore<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            for (var i = initialState; condition(i); i = iterate(i))\n            {\n                yield return resultSelector(i);\n            }\n        }\n    }\n}\n","sourceCodeStart":8,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs#L8-L42","documentation":"Generate requires an iterate function to advance the generator state each loop iteration. A null iterate would make the loop unable to progress, so it is validated eagerly and ArgumentNullException with param name 'iterate' is thrown.","triggerScenarios":"Calling Generate(initialState, condition, null, resultSelector), typically with a state-transition delegate that is null because it was never assigned or a lookup returned null.","commonSituations":"State-machine steps configured externally; passing a method group whose method is missing after an API rename; deserialized delegate fields left null.","solutions":["Provide a real state transition, e.g. state => state + 1.","Coalesce: iterate ?? (s => s) if the state should remain constant (careful: causes infinite loop unless condition terminates).","Fix the null-producing source of the delegate."],"exampleFix":"// before\nvar seq = GeneratorEx.Generate(0, c, step, x => x); // step is null\n// after\nvar seq = GeneratorEx.Generate(0, c, step ?? (s => s + 1), x => x);","handlingStrategy":"validation","validationCode":"if (iterate is null) throw new ArgumentException(\"iterate function is required\");","typeGuard":"bool HasStepper<TState>(Func<TState, TState>? f) => f is not null;","tryCatchPattern":"try { var seq = GeneratorEx.Generate(state, condition, iterate, selector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"iterate\") { log.Error(\"state transition function missing\"); }","preventionTips":["Always supply a state transition, even a trivial s => s, when prototyping.","Check that method groups referenced for the step function still exist after renames.","Validate configured step functions at configuration load time."],"tags":["null-argument","linq","generator"],"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"}