{"record":{"id":"23e29075c80dc244","repo":"dotnet/reactive","slug":"argumentoutofrangeexception-observable","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs","lineNumber":326,"sourceCode":"        /// <param name=\"capacity\">The initial number of elements that the underlying dictionary can contain.</param>\n        /// <returns>A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"keySelector\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"capacity\"/> is less than 0.</exception>\n        public static IObservable<IGroupedObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IObservable<TSource> source, Func<TSource, TKey> keySelector, int capacity)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (keySelector == null)\n            {\n                throw new ArgumentNullException(nameof(keySelector));\n            }\n\n            if (capacity < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n            }\n\n            return s_impl.GroupBy(source, keySelector, capacity);\n        }\n\n        /// <summary>\n        /// Groups the elements of an observable sequence with the specified initial capacity according to a specified key selector function and comparer.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TKey\">The type of the grouping key computed for each element in the source sequence.</typeparam>\n        /// <param name=\"source\">An observable sequence whose elements to group.</param>\n        /// <param name=\"keySelector\">A function to extract the key for each element.</param>\n        /// <param name=\"capacity\">The initial number of elements that the underlying dictionary can contain.</param>\n        /// <param name=\"comparer\">An equality comparer to compare keys with.</param>\n        /// <returns>A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"keySelector\"/> or <paramref name=\"comparer\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"capacity\"/> is less than 0.</exception>\n        public static IObservable<IGroupedObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IObservable<TSource> source, Func<TSource, TKey> keySelector, int capacity, IEqualityComparer<TKey> comparer)","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs#L308-L344","documentation":"ArgumentOutOfRangeException thrown by GroupBy(source, keySelector, capacity) when capacity is negative. Capacity is a hint for pre-allocating internal group storage and must be >= 0; the library treats a negative value as a caller bug and fails fast.","triggerScenarios":"Calling source.GroupBy(keySelector, capacity) with capacity < 0, e.g. computing capacity from a subtraction or a config value that ended up negative (line 326 is the capacity check block).","commonSituations":"Deriving capacity from `max - min` where inputs were swapped; reading a sizing hint from appSettings where a sentinel -1 meant 'auto' but the API has no auto mode.","solutions":["Pass a non-negative capacity, e.g. Math.Max(0, configuredCapacity).","If capacity is unknown, use the overload without the capacity parameter.","Clamp or validate any config-derived value before passing it."],"exampleFix":"// before\nvar g = source.GroupBy(x => x.Key, expected - actual); // may be negative\n// after\nvar g = source.GroupBy(x => x.Key, Math.Max(0, expected - actual));","handlingStrategy":"validation","validationCode":"if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity), capacity, \"capacity must be >= 0\");\nvar groups = source.GroupBy(keySelector, capacity);","typeGuard":"static bool IsValidCapacity(int capacity) => capacity >= 0;","tryCatchPattern":"try { var g = source.GroupBy(k, capacity); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"capacity\") { var g = source.GroupBy(k); }","preventionTips":["Clamp config- or computation-derived capacities with Math.Max(0, value).","Use the parameterless-capacity overload when the size hint is unknown.","Validate sizing settings at application startup."],"tags":["argument-out-of-range","capacity","rx","groupby"],"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"}