{"record":{"id":"c31bcfa3336b96d6","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-c31bcf","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'count')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'count'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/TakeLast.cs","lineNumber":24,"sourceCode":"\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n#if !(REFERENCE_ASSEMBLY && (NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER ))\n        /// <summary>\n        /// Returns a specified number of contiguous elements from the end of the sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"count\">The number of elements to take from the end of the sequence.</param>\n        /// <returns>Sequence with the specified number of elements counting from the end of the source sequence.</returns>\n        public static IEnumerable<TSource> TakeLast<TSource>(this IEnumerable<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 TakeLastCore(source, count);\n        }\n\n        private static IEnumerable<TSource> TakeLastCore<TSource>(IEnumerable<TSource> source, int count)\n        {\n            if (count == 0)\n            {\n                yield break;\n            }\n\n            var q = new Queue<TSource>(count);\n\n            foreach (var item in source)\n            {\n                if (q.Count >= count)\n                {\n                    q.Dequeue();","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/TakeLast.cs#L6-L42","documentation":"TakeLast requires a non-negative `count` because it buffers the last N elements using a queue of bounded size. A negative count has no meaning, so the operator eagerly throws ArgumentOutOfRangeException naming `count` when called with count < 0.","triggerScenarios":"Calling `seq.TakeLast(-1)` or passing a computed length that can go negative, e.g. `TakeLast(list.Count - other.Count)` when other.Count > list.Count.","commonSituations":"Arithmetic on sizes taken from user input or config (pageSize = -1), off-by-one/diff calculations between two collections, or subtracting a baseline from a length.","solutions":["Clamp with Math.Max(0, count) before calling TakeLast.","Validate the count at the boundary (config, request) and reject negatives with a clear message.","Fix the arithmetic producing the negative number.","Note: count == 0 is valid and yields an empty sequence — only negatives throw."],"exampleFix":"// before\nvar last = data.TakeLast(data.Count - removed);\n// after\nvar last = data.TakeLast(Math.Max(0, data.Count - removed));","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));\n// or clamp: var safeCount = Math.Max(0, count);","typeGuard":null,"tryCatchPattern":"try { var last = seq.TakeLast(count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* clamp and retry or fail fast */ }","preventionTips":["Clamp computed counts with Math.Max(0, ...).","Validate pageSize/count config values at load time.","Be careful with subtraction-based counts (a.Count - b.Count).","Unit-test edge inputs: 0, 1, and negative counts."],"tags":["linq","argument-out-of-range","ix-interactive"],"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"}