{"record":{"id":"5288dd3deb0849f7","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-range-of-valid-values","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'count')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'count'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Take.cs","lineNumber":19,"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.Reactive.Concurrency;\nusing System.Reactive.Disposables;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Take<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<TSource>();\n            }\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                count,\n                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.Take(observer, count)));\n        }\n\n        public static IAsyncObservable<TSource> Take<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":37,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Take.cs#L1-L37","documentation":"ArgumentOutOfRangeException for 'count' in AsyncObservable.Take: the requested number of elements is outside the valid range (negative counts are rejected; zero is allowed and yields an empty sequence). The guard fires synchronously at query construction, before any subscription.","triggerScenarios":"Calling source.Take(-1) or any negative count, often from a computed value such as pageSize - skip that went negative.","commonSituations":"Paging logic where the remaining-item calculation underflows (e.g. take = total - offset with offset > total), or unvalidated user input for a limit.","solutions":["Clamp the count to zero or more: Math.Max(0, count)","Validate the input value before calling Take","Fix the upstream arithmetic that produced the negative number"],"exampleFix":"// before\nvar page = source.Take(total - offset); // can be negative\n// after\nvar page = source.Take(Math.Max(0, total - offset));","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));\nvar taken = source.Take(count);","typeGuard":"bool IsValidCount(int count) => count >= 0;","tryCatchPattern":"try { var taken = source.Take(count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* clamp and retry */ }","preventionTips":["Clamp computed counts with Math.Max(0, value)","Validate user-supplied limits at the boundary","Watch paging arithmetic for underflow (skip > total)"],"tags":["argument-out-of-range","linq","async-rx"],"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"}