{"record":{"id":"86b886627903f2b0","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'capacity')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'capacity'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs","lineNumber":243,"sourceCode":"            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TElement>>.From(\n                source,\n                (keySelector, elementSelector, comparer),\n                static (source, state, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, state.keySelector, state.elementSelector, state.comparer)));\n        }\n\n        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, 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 (elementSelector == null)\n                throw new ArgumentNullException(nameof(elementSelector));\n            if (capacity < 0)\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n\n            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TElement>>.From(\n                source,\n                (keySelector, elementSelector, capacity),\n                static (source, state, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, state.keySelector, state.elementSelector, state.capacity)));\n        }\n\n        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, 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 (elementSelector == null)\n                throw new ArgumentNullException(nameof(elementSelector));\n            if (capacity < 0)\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n            if (comparer == null)","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs#L225-L261","documentation":"This GroupBy overload accepts an initial capacity hint for the per-group dictionaries. Because a negative capacity is meaningless, the operator validates it up front and throws ArgumentOutOfRangeException naming 'capacity'. The value is only a sizing hint, so any non-negative value is accepted.","triggerScenarios":"Calling AsyncObservable.GroupBy(source, keySelector, elementSelector, capacity) with a negative capacity value, typically computed from a variable (e.g. list.Count - offset when offset > Count).","commonSituations":"Capacity computed from user input, configuration, or subtraction that can go negative; passing -1 as a 'default' sentinel; integer arithmetic on sizes that underflows.","solutions":["Clamp or validate the capacity before the call: if (capacity < 0) capacity = 0; or use Math.Max(0, n).","Omit the capacity argument and use an overload without it (defaults to int.MaxValue).","Fix the upstream computation that produced the negative number."],"exampleFix":"// before\nvar grouped = source.GroupBy(x => x.Key, x => x, items.Length - skipped);\n// after\nvar capacity = Math.Max(0, items.Length - skipped);\nvar grouped = source.GroupBy(x => x.Key, x => x, capacity);","handlingStrategy":"validation","validationCode":"if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity), capacity, \"Capacity must be non-negative.\");\nvar grouped = source.GroupBy(keySelector, elementSelector, capacity);","typeGuard":"bool IsValidCapacity(int capacity) => capacity >= 0;","tryCatchPattern":"try\n{\n    grouped = source.GroupBy(keySelector, elementSelector, capacity);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"capacity\")\n{\n    grouped = source.GroupBy(keySelector, elementSelector); // default capacity\n}","preventionTips":["Clamp computed capacities with Math.Max(0, n).","Avoid subtraction-based size calculations without a lower-bound check.","Prefer overloads without capacity unless profiling shows a real need."],"tags":["argument-out-of-range","capacity","groupby","asyncrx"],"backgroundTag":"argument-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"}