{"record":{"id":"98073516f3810e8c","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-980735","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":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":457,"sourceCode":"        }\n\n        #endregion\n\n        #region + Range +\n\n        /// <summary>\n        /// Generates an observable sequence of integral numbers within a specified range.\n        /// </summary>\n        /// <param name=\"start\">The value of the first integer in the sequence.</param>\n        /// <param name=\"count\">The number of sequential integers to generate.</param>\n        /// <returns>An observable sequence that contains a range of sequential integral numbers.</returns>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"count\"/> is less than zero. -or- <paramref name=\"start\"/> + <paramref name=\"count\"/> - 1 is larger than <see cref=\"int.MaxValue\"/>.</exception>\n        public static IObservable<int> Range(int start, int count)\n        {\n            var max = (long)start + count - 1;\n            if (count < 0 || max > int.MaxValue)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count));\n            }\n\n            return s_impl.Range(start, count);\n        }\n\n        /// <summary>\n        /// Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.\n        /// </summary>\n        /// <param name=\"start\">The value of the first integer in the sequence.</param>\n        /// <param name=\"count\">The number of sequential integers to generate.</param>\n        /// <param name=\"scheduler\">Scheduler to run the generator loop on.</param>\n        /// <returns>An observable sequence that contains a range of sequential integral numbers.</returns>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"count\"/> is less than zero. -or- <paramref name=\"start\"/> + <paramref name=\"count\"/> - 1 is larger than <see cref=\"int.MaxValue\"/>.</exception>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"scheduler\"/> is null.</exception>\n        public static IObservable<int> Range(int start, int count, IScheduler scheduler)\n        {\n            var max = (long)start + count - 1;\n            if (count < 0 || max > int.MaxValue)","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L439-L475","documentation":"Observable.Range throws ArgumentOutOfRangeException ('count') when count is negative, or when start + count - 1 exceeds int.MaxValue (i.e. the requested integer range would overflow). Rx computes the upper bound as a long to detect overflow and rejects the request before generating anything.","triggerScenarios":"Calling Observable.Range(start, count) with count < 0, or with a start/count pair whose end exceeds int.MaxValue, e.g. Range(0, int.MaxValue + 1 impossible via int but Range(int.MaxValue, 2) overflows; Range(-5, -1) negative count.","commonSituations":"Computing count dynamically (e.g. from pageSize or a user input) without clamping; mixing up total counts with offsets; integer overflow when deriving count from timestamps or file sizes.","solutions":["Ensure count >= 0 and start + count - 1 <= int.MaxValue before calling","Clamp count to the maximum legal value: Math.Min(count, int.MaxValue - start + 1)","Fix the caller that computes count so negatives/overflows cannot reach Range"],"exampleFix":"// before\nObservable.Range(start, count) // count may overflow past int.MaxValue\n// after\nvar safeCount = Math.Min(count, int.MaxValue - Math.Max(start, 0) + 1);\nif (safeCount >= 0) Observable.Range(start, safeCount);","handlingStrategy":"validation","validationCode":"if (count < 0 || (long)start + count - 1 > int.MaxValue)\n    throw new ArgumentOutOfRangeException(nameof(count));","typeGuard":"bool IsValidRange(int start, int count) => count >= 0 && (long)start + count - 1 <= int.MaxValue;","tryCatchPattern":"try { Observable.Range(start, count); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* clamp or correct count */ }","preventionTips":["Clamp count with Math.Min(count, int.MaxValue - start + 1)","Validate user/config-driven counts before passing to Range","Watch for overflow when count is computed from other quantities"],"tags":["reactive","argument-out-of-range","linq"],"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"}