{"record":{"id":"89228d5e75abd855","repo":"dotnet/reactive","slug":"new-argumentnullexception-nameof-keyselector","errorCode":null,"errorMessage":"new ArgumentNullException(nameof(keySelector))","messagePattern":"new ArgumentNullException\\(nameof\\(keySelector\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.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.Collections.Generic;\nusing System.Reactive.Disposables;\nusing System.Reactive.Subjects;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n\n            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(\n                source,\n                keySelector,\n                static (source, keySelector, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, keySelector)));\n        }\n\n        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(\n                source,","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs#L1-L37","documentation":"GroupBy requires a keySelector (Func<TSource, TKey>) to compute each element's group key. A null selector would make grouping impossible, so the library throws ArgumentNullException(nameof(keySelector)) right after validating the source.","triggerScenarios":"Calling source.GroupBy(null) or passing a null delegate through from a helper/lambda factory — e.g. a key-extraction function resolved from configuration or a method group that failed to bind.","commonSituations":"Key mapping policy not configured; generic grouping helper that forwards an optional selector defaulting to null; refactoring that left the selector argument empty in a positional call.","solutions":["Pass a concrete key selector, e.g. x => x.CategoryId.","If all elements belong to one group, use a constant selector x => 0 rather than null.","Null-check the configured selector in your wrapper before delegating to GroupBy."],"exampleFix":"// before\nvar groups = source.GroupBy(null);\n// after\nvar groups = source.GroupBy(x => x.Key);","handlingStrategy":"validation","validationCode":"if (keySelector is null) throw new ArgumentNullException(nameof(keySelector));","typeGuard":"static bool IsValidKeySelector<TSource, TKey>(Func<TSource, TKey>? k) => k is not null;","tryCatchPattern":"try { var groups = source.GroupBy(keySelector); } catch (ArgumentNullException ex) when (ex.ParamName == \"keySelector\") { keySelector = static _ => default!; }","preventionTips":["Load key-mapping policies from config with null validation","Use constant selectors for single-group scenarios instead of null","Prefer named arguments when calling GroupBy in generic wrappers"],"tags":["argument-null","null-check","groupby","key-selector"],"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"}