dotnet/reactive · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values…

Error message

Specified argument was out of the range of valid values. (Parameter 'repeatCount')

What it means

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.

Solutions

  1. Clamp the count before the call: repeatCount = Math.Max(0, repeatCount).
  2. Validate user/config-sourced counts with a range check and a clear error message.
  3. If an infinite or large repetition is intended, use the parameterless-count overload Observable.Repeat(value) instead.
  4. Fix the upstream computation that produced the negative count (e.g. guard subtraction results).

Example fix

// before
int count = items.Count - removed;
var xs = Observable.Repeat(42, count); // can be negative
// after
int count = Math.Max(0, items.Count - removed);
var xs = Observable.Repeat(42, count);
Defensive patterns

Strategy: validation

Validate before calling

if (repeatCount < 0)
    throw new ArgumentOutOfRangeException(nameof(repeatCount), repeatCount, "Must be >= 0.");
var xs = Observable.Repeat(value, repeatCount);

Type guard

static bool IsValidRepeatCount(int n) => n >= 0;

Try / catch

try { var xs = Observable.Repeat(value, repeatCount); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "repeatCount")
{ var xs = Observable.Repeat(value, Math.Max(0, repeatCount)); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/237eb215d3089935. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:533

                throw new ArgumentNullException(nameof(scheduler));
            }

            return s_impl.Repeat(value, scheduler);
        }

        /// <summary>
        /// Generates an observable sequence that repeats the given element the specified number of times.
        /// </summary>
        /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
        /// <param name="value">Element to repeat.</param>
        /// <param name="repeatCount">Number of times to repeat the element.</param>
        /// <returns>An observable sequence that repeats the given element the specified number of times.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="repeatCount"/> is less than zero.</exception>
        public static IObservable<TResult> Repeat<TResult>(TResult value, int repeatCount)
        {
            if (repeatCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(repeatCount));
            }

            return s_impl.Repeat(value, repeatCount);
        }

        /// <summary>
        /// Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.
        /// </summary>
        /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
        /// <param name="value">Element to repeat.</param>
        /// <param name="repeatCount">Number of times to repeat the element.</param>
        /// <param name="scheduler">Scheduler to run the producer loop on.</param>
        /// <returns>An observable sequence that repeats the given element the specified number of times.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="repeatCount"/> is less than zero.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
        public static IObservable<TResult> Repeat<TResult>(TResult value, int repeatCount, IScheduler scheduler)
        {
            if (repeatCount < 0)

View on GitHub (pinned to 94b5d5ab91)