{"record":{"id":"bfbaa7dc39ab554a","repo":"dotnet/reactive","slug":"new-argumentnullexception-nameof-comparer","errorCode":null,"errorMessage":"new ArgumentNullException(nameof(comparer))","messagePattern":"new ArgumentNullException\\(nameof\\(comparer\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs","lineNumber":34,"sourceCode":"            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,\n                (keySelector, comparer),\n                static (source, state, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, state.keySelector, state.comparer)));\n        }\n\n        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, int capacity)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (capacity < 0)\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n\n            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(\n                source,","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs#L16-L52","documentation":"The three-argument GroupBy(source, keySelector, comparer) throws ArgumentNullException when comparer is null. A non-null IEqualityComparer<TKey> is required to route elements into groups; unlike plain LINQ-to-Objects, this overload does not fall back to EqualityComparer<TKey>.Default. Validation is performed eagerly at call time.","triggerScenarios":"Calling GroupBy(source, keySelector, null) — usually a comparer resolved from configuration, DI container, or a lookup that returned null.","commonSituations":"DI-registered comparers missing from the container; comparer selected conditionally with no default branch; passing a custom comparer class instance that a factory failed to create.","solutions":["Pass a non-null IEqualityComparer<TKey>, e.g. EqualityComparer<TKey>.Default if default semantics are desired.","Fix the comparer factory/registration so it never returns null.","Coalesce: comparer ?? EqualityComparer<TKey>.Default before the call."],"exampleFix":"// before\nvar groups = source.GroupBy(x => x.Key, resolver.GetComparer<string>()); // may be null\n// after\nvar groups = source.GroupBy(x => x.Key, resolver.GetComparer<string>() ?? EqualityComparer<string>.Default);","handlingStrategy":"validation","validationCode":"if (comparer is null) comparer = EqualityComparer<TKey>.Default;","typeGuard":"static bool HasComparer<TKey>(IEqualityComparer<TKey> c) => c is not null;","tryCatchPattern":"try { var groups = source.GroupBy(keySelector, comparer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"comparer\") { log.LogError(ex, \"GroupBy comparer was null\"); throw; }","preventionTips":["Coalesce comparer ?? EqualityComparer<TKey>.Default at call sites.","Verify DI registrations for custom comparers at container build time.","Prefer passing EqualityComparer<TKey>.Default explicitly over null."],"tags":["null-argument","linq","argument-validation"],"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"}