{"record":{"id":"efc17369a324f810","repo":"dotnet/reactive","slug":"count-takelastbuffer","errorCode":null,"errorMessage":"count","messagePattern":"count","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLastBuffer.cs","lineNumber":18,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Reactive.Concurrency;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<IList<TSource>> TakeLastBuffer<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            if (count == 0)\n            {\n                return Empty<IList<TSource>>();\n            }\n\n            return CreateAsyncObservable<IList<TSource>>.From(\n                source,\n                count,\n                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, count)));\n        }\n\n        public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (duration < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(duration));","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLastBuffer.cs#L1-L36","documentation":"ArgumentOutOfRangeException whose parameter name is 'count', thrown by TakeLastBuffer's range guard: the number of trailing elements to buffer must be non-negative. A negative count makes the end-of-sequence window undefined, so the operator rejects it up front.","triggerScenarios":"source.TakeLastBuffer(-1) — negative values from subtraction, unsigned conversion, or unvalidated configuration.","commonSituations":"Buffer size computed as newer - older with wrong order; config-driven batch size that went negative; sentinel value -1 for 'unset'.","solutions":["Pass a non-negative count (0 is allowed and yields an empty buffer).","Clamp: Math.Max(0, n) before the call.","Validate the configured buffer size at startup."],"exampleFix":"// before\nvar buf = source.TakeLastBuffer(size); // can be -1\n// after\nvar buf = source.TakeLastBuffer(Math.Max(0, size));","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));\nsource.TakeLastBuffer(count);","typeGuard":null,"tryCatchPattern":"try { var buf = source.TakeLastBuffer(count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* clamp to 0 */ }","preventionTips":["Clamp with Math.Max(0, n); zero is legal here.","Check subtraction order when computing sizes from differences.","Reject negative configured sizes at load time."],"tags":["argument-out-of-range","asyncrx","takelastbuffer","count"],"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"}