{"record":{"id":"a323465e7124ce57","repo":"dotnet/reactive","slug":"nameof-condition-generate","errorCode":null,"errorMessage":"nameof(condition)","messagePattern":"nameof\\(condition\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs","lineNumber":15,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Concurrency;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<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 Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));\n        }\n\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)\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            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs#L1-L33","documentation":"The public AsyncObservable.Generate overload requires a condition predicate and throws ArgumentNullException with the parameter name 'condition' when it is null. Generate drives a for-loop-like async sequence, so a null condition cannot be substituted by a library default.","triggerScenarios":"Calling AsyncObservable.Generate(initialState, null, iterate, resultSelector) — passing null for the Func<TState,bool> condition argument, often because the predicate is built dynamically or returned null from a factory.","commonSituations":"Condition lambdas supplied via optional config that defaults to null; refactoring that lost the predicate; dependency-injected delegates not registered and resolving to null.","solutions":["Pass a non-null predicate, e.g. x => x < 10, as the condition argument","If the condition is computed, guard with ?? throw before calling Generate","Ensure any factory/DI source producing the predicate actually returns a delegate"],"exampleFix":"// before\nAsyncObservable.Generate(0, null, x => x + 1, x => x);\n// after\nAsyncObservable.Generate(0, x => x < 10, x => x + 1, x => x);","handlingStrategy":"validation","validationCode":"if (condition == null)\n    throw new ArgumentNullException(nameof(condition), \"Generate requires a condition predicate\");\nvar obs = AsyncObservable.Generate(state, condition, iterate, resultSelector);","typeGuard":"static bool IsValidGenerateArgs<TState, TResult>(Func<TState, bool>? condition, Func<TState, TState>? iterate, Func<TState, TResult>? resultSelector)\n    => condition != null && iterate != null && resultSelector != null;","tryCatchPattern":"try\n{\n    var obs = AsyncObservable.Generate(state, condition, iterate, selector);\n}\ncatch (ArgumentNullException ex)\n{\n    logger.LogError(ex, \"Generate received a null delegate: {Param}\", ex.ParamName);\n}","preventionTips":["Never pass nullable delegate variables to Generate without null checks","Remember argument order: initialState, condition, iterate, resultSelector","Use lambdas inline where possible so the compiler guarantees non-null"],"tags":["null-argument","argumentnull","generate"],"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"}