{"record":{"id":"239e7ce79f90347d","repo":"dotnet/reactive","slug":"argumentoutofrangeexception-observable-single","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Single.cs","lineNumber":99,"sourceCode":"        /// <summary>\n        /// Projects each element of an observable sequence into consecutive non-overlapping buffers which are produced based on element count information.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>\n        /// <param name=\"source\">Source sequence to produce buffers over.</param>\n        /// <param name=\"count\">Length of each buffer.</param>\n        /// <returns>An observable sequence of buffers.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"count\"/> is less than or equal to zero.</exception>\n        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, int count)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (count <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count));\n            }\n\n            return s_impl.Buffer(source, count);\n        }\n\n        /// <summary>\n        /// Projects each element of an observable sequence into zero or more buffers which are produced based on element count information.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>\n        /// <param name=\"source\">Source sequence to produce buffers over.</param>\n        /// <param name=\"count\">Length of each buffer.</param>\n        /// <param name=\"skip\">Number of elements to skip between creation of consecutive buffers.</param>\n        /// <returns>An observable sequence of buffers.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"count\"/> or <paramref name=\"skip\"/> is less than or equal to zero.</exception>\n        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, int count, int skip)\n        {\n            if (source == null)","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Single.cs#L81-L117","documentation":"Buffer(source, count) also validates that `count` is strictly positive; count <= 0 throws ArgumentOutOfRangeException synchronously. A zero or negative batch size is meaningless because the operator would never (or instantly) complete a buffer.","triggerScenarios":"Calling source.Buffer(count) with count == 0 or count < 0 — typically a variable read from configuration, parsed input, or computed value that was not validated.","commonSituations":"Config files with batchSize: 0 or missing values parsed to 0; user-supplied batch sizes; off-by-one or division-derived sizes that can evaluate to zero.","solutions":["Validate count > 0 before calling Buffer and surface a friendly message.","Clamp or default: Math.Max(1, configuredSize).","Fix the configuration/parsing path producing the non-positive value."],"exampleFix":"// before\nvar batches = source.Buffer(batchSize); // batchSize from config, may be 0\n// after\nif (batchSize <= 0) throw new ArgumentException(\"Batch size must be positive\", nameof(batchSize));\nvar batches = source.Buffer(batchSize);","handlingStrategy":"validation","validationCode":"if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count), \"Count must be positive\");","typeGuard":"static bool IsValidBufferSize(int count) => count > 0;","tryCatchPattern":"try { var batches = source.Buffer(count); }\ncatch (ArgumentOutOfRangeException ex) { logger.LogError(ex, \"Buffer count must be positive\"); }","preventionTips":["Validate configured batch sizes are > 0 at startup.","Clamp parsed values with Math.Max(1, value).","Add contract tests for boundary config values like 0 and -1."],"tags":["csharp","rx","argument-range","buffer"],"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-23T08:17:48.524Z"}