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 'count')
What it means
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.
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
Example fix
// before Observable.Range(start, count) // count may overflow past int.MaxValue // after var safeCount = Math.Min(count, int.MaxValue - Math.Max(start, 0) + 1); if (safeCount >= 0) Observable.Range(start, safeCount);
Defensive patterns
Strategy: validation
Validate before calling
if (count < 0 || (long)start + count - 1 > int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(count)); Type guard
bool IsValidRange(int start, int count) => count >= 0 && (long)start + count - 1 <= int.MaxValue;
Try / catch
try { Observable.Range(start, count); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* clamp or correct count */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- index
- Value cannot be null. (Parameter 'func')
- new ArgumentOutOfRangeException(nameof(capacity))
- ArgumentOutOfRangeException: capacity
- period
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/98073516f3810e8c.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:457
}
#endregion
#region + Range +
/// <summary>
/// Generates an observable sequence of integral numbers within a specified range.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="count">The number of sequential integers to generate.</param>
/// <returns>An observable sequence that contains a range of sequential integral numbers.</returns>
/// <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>
public static IObservable<int> Range(int start, int count)
{
var max = (long)start + count - 1;
if (count < 0 || max > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
return s_impl.Range(start, count);
}
/// <summary>
/// Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="count">The number of sequential integers to generate.</param>
/// <param name="scheduler">Scheduler to run the generator loop on.</param>
/// <returns>An observable sequence that contains a range of sequential integral numbers.</returns>
/// <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>
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
public static IObservable<int> Range(int start, int count, IScheduler scheduler)
{
var max = (long)start + count - 1;
if (count < 0 || max > int.MaxValue)View on GitHub (pinned to 94b5d5ab91)