{"record":{"id":"113fa78784396018","repo":"dotnet/reactive","slug":"throw-new-argumentoutofrangeexception-nameof-timespan","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException(nameof(timeSpan));","messagePattern":"throw new ArgumentOutOfRangeException\\(nameof\\(timeSpan\\)\\);","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs","lineNumber":199,"sourceCode":"        /// <param name=\"count\">Maximum element count of a window.</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=\"timeSpan\"/> is less than TimeSpan.Zero. -or- <paramref name=\"count\"/> is less than or equal to zero.</exception>\n        /// <remarks>\n        /// 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>","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs#L181-L217","documentation":"The timeSpan/count Buffer overload throws ArgumentOutOfRangeException when timeSpan is negative. Buffer windows are durations used to schedule buffer closings, so a negative TimeSpan has no meaning. The operator validates this eagerly before subscribing.","triggerScenarios":"Calling Observable.Buffer(source, timeSpan, count) with timeSpan < TimeSpan.Zero, e.g. TimeSpan.FromMilliseconds(-1) or a computed duration that went negative due to clock subtraction.","commonSituations":"Computing a duration by subtracting DateTime/Stopwatch values where the end precedes the start; misconfigured timeout settings loaded as negative values; sign errors in TimeSpan arithmetic.","solutions":["Pass a positive TimeSpan, e.g. TimeSpan.FromSeconds(1)","Clamp computed durations with TimeSpan.FromTicks(Math.Max(0, ticks)) before passing","Validate configuration values at startup so negative timeouts never reach Rx operators"],"exampleFix":"// before\nvar ts = end - start; // can be negative\nvar buffered = source.Buffer(ts, 10);\n// after\nvar ts = end - start;\nif (ts < TimeSpan.Zero) ts = TimeSpan.Zero;\nvar buffered = source.Buffer(ts, 10);","handlingStrategy":"validation","validationCode":"if (timeSpan < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeSpan));\n// or clamp: timeSpan = timeSpan < TimeSpan.Zero ? TimeSpan.Zero : timeSpan;","typeGuard":null,"tryCatchPattern":"try { var b = source.Buffer(timeSpan, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"timeSpan\") { /* correct the window and retry */ }","preventionTips":["Clamp any computed duration with Math.Max(TimeSpan.Zero, value)","Validate TimeSpan settings when loading configuration","Watch the operand order when subtracting DateTime/Stopwatch values"],"tags":["rx","argument-validation","argument-out-of-range","timespan"],"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"}