{"record":{"id":"cb4f0138e252359c","repo":"dotnet/reactive","slug":"throw-new-argumentoutofrangeexception-nameof-count","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException(nameof(count));","messagePattern":"throw new ArgumentOutOfRangeException\\(nameof\\(count\\)\\);","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs","lineNumber":204,"sourceCode":"        /// Specifying a TimeSpan.Zero value for <paramref name=\"timeSpan\"/> is not recommended but supported, causing the scheduler to create buffers as fast as it can.\n        /// Because all source sequence elements end up in one of the buffers, some buffers won't have a zero time span. This is a side-effect of the asynchrony introduced\n        /// by the scheduler, where the action to close the current buffer and to create a new buffer may not execute immediately, despite the TimeSpan.Zero due time.\n        /// </remarks>\n        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, TimeSpan timeSpan, int count)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (timeSpan < TimeSpan.Zero)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeSpan));\n            }\n\n            if (count <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count));\n            }\n\n            return s_impl.Buffer(source, timeSpan, count);\n        }\n\n        /// <summary>\n        /// Projects each element of an observable sequence into a buffer that's sent out when either it's full or a given amount of time has elapsed, using the specified scheduler to run timers.\n        /// A useful real-world analogy of this overload is the behavior of a ferry leaving the dock when all seats are taken, or at the scheduled time of departure, whichever event occurs first.\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=\"timeSpan\">Maximum time length of a buffer.</param>\n        /// <param name=\"count\">Maximum element count of a buffer.</param>\n        /// <param name=\"scheduler\">Scheduler to run buffering timers on.</param>\n        /// <returns>An observable sequence of buffers.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"timeSpan\"/> is less than TimeSpan.Zero. -or- <paramref name=\"count\"/> is less than or equal to zero.</exception>\n        /// <remarks>","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs#L186-L222","documentation":"The timeSpan/count Buffer overload throws ArgumentOutOfRangeException when count is zero or negative. count is the maximum number of elements per buffer and must be a positive integer. Validation is performed eagerly at the call site.","triggerScenarios":"Calling Observable.Buffer(source, timeSpan, count) with count <= 0 — e.g. a batch-size config of 0, an empty page-size, or integer arithmetic that produced 0 or a negative number.","commonSituations":"Batch-size settings read from config files where 0 means 'unbounded' in user code but is invalid here; division results rounding to zero; uninitialized int fields defaulting to 0.","solutions":["Pass a positive integer for count (>= 1)","Validate/clamp the batch size before calling: if (count < 1) count = 1;","Fix configuration loading so unset batch sizes get a sane default instead of 0"],"exampleFix":"// before\nint count = config.BatchSize; // could be 0\nvar buffered = source.Buffer(TimeSpan.FromSeconds(1), count);\n// after\nint count = Math.Max(1, config.BatchSize);\nvar buffered = source.Buffer(TimeSpan.FromSeconds(1), count);","handlingStrategy":"validation","validationCode":"if (count < 1) throw new ArgumentOutOfRangeException(nameof(count));\n// or clamp: count = Math.Max(1, count);","typeGuard":null,"tryCatchPattern":"try { var b = source.Buffer(timeSpan, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { count = 1; /* retry with default */ }","preventionTips":["Clamp configured batch sizes with Math.Max(1, value)","Give batch-size config positive defaults when unset","Validate int inputs coming from user/config sources before Rx calls"],"tags":["rx","argument-validation","argument-out-of-range","buffersize"],"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"}