{"record":{"id":"490854cf8be5740e","repo":"dotnet/reactive","slug":"new-argumentoutofrangeexception-nameof-capacity","errorCode":null,"errorMessage":"new ArgumentOutOfRangeException(nameof(capacity))","messagePattern":"new ArgumentOutOfRangeException\\(nameof\\(capacity\\)\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs","lineNumber":49,"sourceCode":"            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,\n                (keySelector, capacity),\n                static (source, state, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, state.keySelector, state.capacity)));\n        }\n\n        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, int capacity, 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 (capacity < 0)\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs#L31-L67","documentation":"GroupBy(source, keySelector, capacity) throws ArgumentOutOfRangeException when capacity is negative. Capacity presets the internal storage for groups; a negative value is meaningless and is rejected up front. Zero is accepted.","triggerScenarios":"Calling GroupBy(source, keySelector, capacity) with capacity < 0 — typically capacity read from config (e.g. '-1' parsed to int), computed as count-1 when the collection is empty, or a default sentinel like -1 meaning 'unset'.","commonSituations":"Config files with negative or placeholder capacity values; arithmetic like items.Count - 1 on empty input; sentinel -1 used for 'auto' but not supported by this API.","solutions":["Pass a non-negative capacity; clamp with Math.Max(0, capacity).","Fix the config/source of the capacity value so it is >= 0.","If capacity is optional, use the two-argument GroupBy overload instead of passing -1."],"exampleFix":"// before\nvar groups = source.GroupBy(x => x.Key, config.BufferSize); // may be -1\n// after\nvar groups = source.GroupBy(x => x.Key, Math.Max(0, config.BufferSize));","handlingStrategy":"validation","validationCode":"if (capacity < 0) throw new InvalidOperationException(\"capacity must be >= 0\");\nvar safeCapacity = Math.Max(0, capacity);","typeGuard":"static bool IsValidCapacity(int capacity) => capacity >= 0;","tryCatchPattern":"try { var groups = source.GroupBy(keySelector, capacity); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"capacity\") { log.LogError(ex, \"GroupBy capacity was negative\"); throw; }","preventionTips":["Clamp config-derived capacities with Math.Max(0, value).","Do not use -1 as an 'auto/unset' sentinel with this API.","Sanitize values computed from collection counts (Count - 1 on empty)."],"tags":["argument-out-of-range","linq","argument-validation"],"backgroundTag":"value-out-of-range","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"}