{"record":{"id":"237eb215d3089935","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-237eb2","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'repeatCount')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'repeatCount'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":533,"sourceCode":"                throw new ArgumentNullException(nameof(scheduler));\n            }\n\n            return s_impl.Repeat(value, scheduler);\n        }\n\n        /// <summary>\n        /// Generates an observable sequence that repeats the given element the specified number of times.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the element that will be repeated in the produced sequence.</typeparam>\n        /// <param name=\"value\">Element to repeat.</param>\n        /// <param name=\"repeatCount\">Number of times to repeat the element.</param>\n        /// <returns>An observable sequence that repeats the given element the specified number of times.</returns>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"repeatCount\"/> is less than zero.</exception>\n        public static IObservable<TResult> Repeat<TResult>(TResult value, int repeatCount)\n        {\n            if (repeatCount < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(repeatCount));\n            }\n\n            return s_impl.Repeat(value, repeatCount);\n        }\n\n        /// <summary>\n        /// Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the element that will be repeated in the produced sequence.</typeparam>\n        /// <param name=\"value\">Element to repeat.</param>\n        /// <param name=\"repeatCount\">Number of times to repeat the element.</param>\n        /// <param name=\"scheduler\">Scheduler to run the producer loop on.</param>\n        /// <returns>An observable sequence that repeats the given element the specified number of times.</returns>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"repeatCount\"/> is less than zero.</exception>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"scheduler\"/> is null.</exception>\n        public static IObservable<TResult> Repeat<TResult>(TResult value, int repeatCount, IScheduler scheduler)\n        {\n            if (repeatCount < 0)","sourceCodeStart":515,"sourceCodeEnd":551,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L515-L551","documentation":"Observable.Repeat(value, repeatCount) validates that repeatCount is not negative and throws ArgumentOutOfRangeException naming 'repeatCount' otherwise. The library treats a negative count as meaningless (zero means an empty sequence, not negative). The check happens eagerly when the factory method is called.","triggerScenarios":"Calling Observable.Repeat<T>(T value, int repeatCount) with repeatCount < 0, e.g. Observable.Repeat(\"x\", -1) or passing a count computed as length - 1 on an empty collection.","commonSituations":"Computing the repeat count from a difference (items.Count - removed) that underflows below zero; user-supplied count parsed from input without lower-bound validation; off-by-one errors turning a valid 0 into -1.","solutions":["Clamp the count before the call: repeatCount = Math.Max(0, repeatCount).","Validate user/config-sourced counts with a range check and a clear error message.","If an infinite or large repetition is intended, use the parameterless-count overload Observable.Repeat(value) instead.","Fix the upstream computation that produced the negative count (e.g. guard subtraction results)."],"exampleFix":"// before\nint count = items.Count - removed;\nvar xs = Observable.Repeat(42, count); // can be negative\n// after\nint count = Math.Max(0, items.Count - removed);\nvar xs = Observable.Repeat(42, count);","handlingStrategy":"validation","validationCode":"if (repeatCount < 0)\n    throw new ArgumentOutOfRangeException(nameof(repeatCount), repeatCount, \"Must be >= 0.\");\nvar xs = Observable.Repeat(value, repeatCount);","typeGuard":"static bool IsValidRepeatCount(int n) => n >= 0;","tryCatchPattern":"try { var xs = Observable.Repeat(value, repeatCount); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"repeatCount\")\n{ var xs = Observable.Repeat(value, Math.Max(0, repeatCount)); }","preventionTips":["Clamp computed counts with Math.Max(0, n).","Validate user/config-sourced counts at the boundary.","Use the parameterless overload for infinite repetition instead of huge counts."],"tags":["rx","argument-out-of-range","repeatcount","argument-validation"],"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"}