{"record":{"id":"4cb6a1b39df47227","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-resultselector","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'resultSelector')","messagePattern":"Value cannot be null\\. \\(Parameter 'resultSelector'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs","lineNumber":68,"sourceCode":"        public static IAsyncObservable<TResult> Aggregate<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, ValueTask<TResult>> func)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TResult>.From(\n                source,\n                (seed, func),\n                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, state.seed, state.func)));\n        }\n\n        public static IAsyncObservable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TResult>.From(\n                source,\n                (seed, func, resultSelector),\n                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, state.seed, state.func, state.resultSelector)));\n        }\n\n        public static IAsyncObservable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> func, Func<TAccumulate, ValueTask<TResult>> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs#L50-L86","documentation":"System.ArgumentNullException thrown by the seeded overload with result selector when 'resultSelector' (Func<TAccumulate,TResult>) is null, guarded at Aggregate.cs:68. Notably this overload checks resultSelector before func, so a null resultSelector is reported even if func is also null.","triggerScenarios":"Calling source.Aggregate(seed, func, null) — the three-delegate form with a null final projection. Also produced when both func and resultSelector are null (resultSelector check runs first).","commonSituations":"Projection delegates kept in nullable members not yet initialized; refactoring that removed the result selector but kept the four-argument call; null passed conditionally for optional projection.","solutions":["Provide a result selector, e.g. source.Aggregate(seed, acc, l => l.Count).","If no projection is needed, switch to the overload without resultSelector.","Substitute an identity-like selector (x => x) if the delegate is conditionally null."],"exampleFix":"// before\nvar result = source.Aggregate(new List<int>(), (acc, x) => { acc.Add(x); return acc; }, maybeSelector); // null\n\n// after\nvar result = source.Aggregate(new List<int>(), (acc, x) => { acc.Add(x); return acc; }, maybeSelector ?? (l => (IReadOnlyList<int>)l));","handlingStrategy":"validation","validationCode":"if (resultSelector is null) throw new ArgumentNullException(nameof(resultSelector));\nvar result = source.Aggregate(seed, func, resultSelector);","typeGuard":"static bool IsValidSelectorArgs<TAccumulate, TResult>(Func<TAccumulate, TResult>? resultSelector) => resultSelector is not null;","tryCatchPattern":"try { var result = source.Aggregate(seed, acc, proj); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\") { /* add identity projection or drop the overload */ }","preventionTips":["If no projection is needed, use the overload without resultSelector instead of passing null.","Coalesce with an identity/projection default when the selector is optional.","Keep projections defined near the aggregation so they are never left unassigned."],"tags":["null-argument","argument-validation","linq","projection"],"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"}