{"record":{"id":"584131ecb57b317b","repo":"dotnet/reactive","slug":"argumentoutofrangeexception-window","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs","lineNumber":21,"sourceCode":"// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Reactive.Concurrency;\nusing System.Reactive.Disposables;\nusing System.Reactive.Subjects;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<IAsyncObservable<TSource>> Window<TSource>(this IAsyncObservable<TSource> source, int count)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count <= 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n\n            return CreateAsyncObservable<IAsyncObservable<TSource>>.From(\n                source,\n                count,\n                static (source, count, observer) => WindowCore(source, observer, (o, d) => AsyncObserver.Window(o, d, count)));\n        }\n\n        public static IAsyncObservable<IAsyncObservable<TSource>> Window<TSource>(this IAsyncObservable<TSource> source, int count, int skip)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count <= 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n            if (skip <= 0)\n                throw new ArgumentOutOfRangeException(nameof(skip));\n\n            return CreateAsyncObservable<IAsyncObservable<TSource>>.From(\n                source,","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs#L3-L39","documentation":"Window<TSource>(source, count) requires count to be strictly positive; count <= 0 raises ArgumentOutOfRangeException. A non-positive window size is meaningless (an empty window can never complete), so the library rejects it eagerly.","triggerScenarios":"Calling source.Window(0), source.Window(-1), or with a count computed from unvalidated input (config value, parsed string) that is <= 0.","commonSituations":"Reading a batch-size setting from configuration that defaults to 0; subtracting offsets and passing a negative remainder; hard-coding a size then changing semantics so it can be zero for 'disabled'.","solutions":["Pass a count >= 1; clamp with Math.Max(1, requestedCount) if the value is configurable.","Validate configuration values at load time and reject non-positive batch/window sizes with a clear message.","Check the computation producing count for off-by-one or sign errors.","If callers may pass 0 meaning 'no windowing', branch to skip the Window operator instead of calling it."],"exampleFix":"// before\nvar windows = source.Window(config.WindowSize); // may be 0\n// after\nif (config.WindowSize <= 0) throw new InvalidOperationException(\"WindowSize must be positive\");\nvar windows = source.Window(config.WindowSize);","handlingStrategy":"validation","validationCode":"if (count <= 0)\n    throw new ArgumentOutOfRangeException(nameof(count), \"Window size must be at least 1\");\nvar windows = source.Window(count);","typeGuard":"static bool IsValidWindowSize(int count) => count > 0;","tryCatchPattern":"try { var windows = source.Window(count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\")\n{\n    var windows = source.Window(1); // or handle invalid config\n}","preventionTips":["Clamp configurable sizes with Math.Max(1, value)","Validate configuration values at startup, not at pipeline build","Treat 0/negative batch sizes as config errors and fail fast","Unit-test pipeline builders with boundary values 0 and 1"],"tags":["csharp","argument-validation","argument-out-of-range","async-rx"],"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"}