{"record":{"id":"b7d6265b11ef9163","repo":"dotnet/reactive","slug":"nameof-condition-observable-creation","errorCode":null,"errorMessage":"nameof(condition)","messagePattern":"nameof\\(condition\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":361,"sourceCode":"\n        #region + Generate +\n\n        /// <summary>\n        /// Generates an observable sequence by running a state-driven loop producing the sequence's elements.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state used in the generator loop.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements in the produced sequence.</typeparam>\n        /// <param name=\"initialState\">Initial state.</param>\n        /// <param name=\"condition\">Condition to terminate generation (upon returning false).</param>\n        /// <param name=\"iterate\">Iteration step function.</param>\n        /// <param name=\"resultSelector\">Selector function for results produced in the sequence.</param>\n        /// <returns>The generated sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"condition\"/> or <paramref name=\"iterate\"/> or <paramref name=\"resultSelector\"/> is null.</exception>\n        public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            if (condition == null)\n            {\n                throw new ArgumentNullException(nameof(condition));\n            }\n\n            if (iterate == null)\n            {\n                throw new ArgumentNullException(nameof(iterate));\n            }\n\n            if (resultSelector == null)\n            {\n                throw new ArgumentNullException(nameof(resultSelector));\n            }\n\n            return s_impl.Generate(initialState, condition, iterate, resultSelector);\n        }\n\n        /// <summary>\n        /// Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.\n        /// </summary>","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L343-L379","documentation":"Observable.Generate throws ArgumentNullException when the 'condition' delegate passed to it is null. Rx requires all three delegates (condition, iterate, resultSelector) to be non-null because the generator loop calls them on every step; a null delegate would cause a NullReferenceException deep inside the operator instead of a clear argument check at the call site.","triggerScenarios":"Calling System.Reactive.Linq.Observable.Generate<TState, TResult>(initialState, null, iterate, resultSelector) — e.g. a condition lambda variable that was never assigned, or a conditional expression that resolved to null.","commonSituations":"Building a generator from config-driven or dynamically composed delegates where an optional predicate was not supplied; refactoring that removed a lambda but left a null variable; DI factories returning null delegates.","solutions":["Pass a non-null condition predicate, e.g. x => x < 10","If the condition is optional, substitute a permissive default like _ => true before calling Generate","Check the variable being passed for null before invoking Generate"],"exampleFix":"// before\nObservable.Generate(0, maybeCondition, i => i + 1, i => i)\n// after\nvar cond = maybeCondition ?? (_ => true);\nObservable.Generate(0, cond, i => i + 1, i => i)","handlingStrategy":"validation","validationCode":"if (condition == null) throw new ArgumentNullException(nameof(condition)); // or guard before calling","typeGuard":"bool HasCondition(Func<TState,bool> c) => c is not null;","tryCatchPattern":"try { Observable.Generate(state, cond, it, sel); } catch (ArgumentNullException ex) when (ex.ParamName == \"condition\") { /* supply default predicate */ }","preventionTips":["Default optional predicates to _ => true before composing generators","Avoid nullable delegate fields feeding Rx operators","Null-check delegates at configuration load time"],"tags":["reactive","null-argument","linq"],"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"}