{"record":{"id":"e5a419629db9a716","repo":"dotnet/reactive","slug":"nameof-resultselector","errorCode":null,"errorMessage":"nameof(resultSelector)","messagePattern":"nameof\\(resultSelector\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs","lineNumber":19,"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));\n\n            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector, scheduler));\n        }\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs#L1-L37","documentation":"AsyncObservable.Generate throws ArgumentNullException with parameter name 'resultSelector' when the Func<TState,TResult> mapping argument is null. The result selector converts each state into an emitted item, so a null selector is invalid.","triggerScenarios":"Calling AsyncObservable.Generate(initialState, condition, iterate, null) or the scheduler overload with a null fourth argument — often from argument-order mistakes or a selector variable that was never initialized.","commonSituations":"Passing arguments in the wrong order so a null lands in the selector slot; selectors built via reflection/Expression that compile to null; nullable delegate fields not yet assigned.","solutions":["Supply a non-null selector, e.g. x => x * 2, as resultSelector","Verify the parameter order (state, condition, iterate, resultSelector [, scheduler])","Guard the selector at its creation site with ?? throw new ArgumentNullException"],"exampleFix":"// before\nAsyncObservable.Generate(0, x => x < 10, x => x + 1, null);\n// after\nAsyncObservable.Generate(0, x => x < 10, x => x + 1, x => x);","handlingStrategy":"validation","validationCode":"if (resultSelector == null)\n    throw new ArgumentNullException(nameof(resultSelector), \"Generate requires a result selector\");\nvar obs = AsyncObservable.Generate(state, condition, iterate, resultSelector);","typeGuard":"static bool HasSelector<TState, TResult>(Func<TState, TResult>? selector) => selector != null;","tryCatchPattern":"try\n{\n    var obs = AsyncObservable.Generate(state, condition, iterate, selector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\")\n{\n    // correct the argument order or supply a selector\n}","preventionTips":["Count arguments when calling Generate; a missing selector often means an omitted argument","Prefer named/inline lambdas over nullable delegate variables","Write a unit test per Generate call site"],"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"}