{"record":{"id":"5063bfc29a3be73a","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-5063bf","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'count')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'count'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs","lineNumber":225,"sourceCode":"            var inner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n            await d.AssignAsync(inner).ConfigureAwait(false);\n\n            return subscription;\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static (IAsyncObserver<TSource>, IAsyncDisposable) Window<TSource>(IAsyncObserver<IAsyncObservable<TSource>> observer, IAsyncDisposable subscription, int count) => Window(observer, subscription, count, count);\n\n        public static (IAsyncObserver<TSource>, IAsyncDisposable) Window<TSource>(IAsyncObserver<IAsyncObservable<TSource>> observer, IAsyncDisposable subscription, int count, int skip)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (subscription == null)\n                throw new ArgumentNullException(nameof(subscription));\n            if (count <= 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n            if (skip <= 0)\n                throw new ArgumentOutOfRangeException(nameof(skip));\n\n            var refCount = new RefCountAsyncDisposable(subscription);\n\n            var queue = new Queue<IAsyncSubject<TSource>>();\n            var n = 0;\n\n            return\n                (\n                    Create<TSource>\n                    (\n                        async x =>\n                        {\n                            foreach (var window in queue)\n                            {\n                                await window.OnNextAsync(x).ConfigureAwait(false);\n                            }","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs#L207-L243","documentation":"This ArgumentOutOfRangeException is thrown by the count-based Window helper when count is zero or negative. count defines how many source elements go into each window, so only positive integers are valid. The check runs immediately at call time, before subscription.","triggerScenarios":"Calling Window(source, count) or Window(observer, subscription, count[, skip]) with count <= 0, e.g. a computed value of 0 from an empty config or a division result.","commonSituations":"Batch-size configuration defaulting to 0; parsing user input without validating positivity; arithmetic that underflows to 0 or negative when the sequence is empty.","solutions":["Pass a positive count (>= 1) to Window.","Validate/clamp the value first: if (count < 1) throw new ArgumentException(...); or count = Math.Max(1, count).","Fix the configuration source so the batch/window size is a positive number.","Ensure user input is parsed and validated before reaching the operator."],"exampleFix":"// before\nvar size = config.BatchSize; // 0\nvar windows = source.Window(size);\n// after\nvar size = Math.Max(1, config.BatchSize);\nvar windows = source.Window(size);","handlingStrategy":"validation","validationCode":"if (count < 1) throw new ArgumentOutOfRangeException(nameof(count), count, \"Window count must be positive.\");\n// or clamp: count = Math.Max(1, count);","typeGuard":"bool IsValidCount(int count) => count > 0;","tryCatchPattern":"try\n{\n    var windows = source.Window(count);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\")\n{\n    count = DefaultWindowSize; // e.g. 10\n}","preventionTips":["Validate window/batch sizes from configuration at load time.","Never compute sizes with arithmetic that can yield 0 on empty inputs.","Use Math.Clamp or explicit guards for any user-supplied size."],"tags":["argument-out-of-range","argument-validation","async-rx","window-operator"],"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"}