dotnet/csharplang · error · ArgumentOutOfRangeException
{argumentExpression} ({argument}) cannot be greater than {hi
Error message
{argumentExpression} ({argument}) cannot be greater than {highExpression} ({high}). What it means
Thrown by the example `InRange(int argument, int low, int high)` helper from the caller-argument-expression proposal when `argument > high`. As with the low-bound case, the message uses CallerArgumentExpression to print the offending expression, its value, and the upper bound.
Source
Thrown at proposals/csharp-10.0/caller-argument-expression.md:101
public static void Argument(bool condition, string message, [CallerArgumentExpression("condition")] string conditionExpression = null)
{
if (!condition) throw new ArgumentException(message: message, paramName: conditionExpression);
}
public static void InRange(int argument, int low, int high,
[CallerArgumentExpression("argument")] string argumentExpression = null,
[CallerArgumentExpression("low")] string lowExpression = null,
[CallerArgumentExpression("high")] string highExpression = null)
{
if (argument < low)
{
throw new ArgumentOutOfRangeException(paramName: argumentExpression,
message: $"{argumentExpression} ({argument}) cannot be less than {lowExpression} ({low}).");
}
if (argument > high)
{
throw new ArgumentOutOfRangeException(paramName: argumentExpression,
message: $"{argumentExpression} ({argument}) cannot be greater than {highExpression} ({high}).");
}
}
public static void NotNull<T>(T argument, [CallerArgumentExpression("argument")] string argumentExpression = null)
where T : class
{
if (argument == null) throw new ArgumentNullException(paramName: argumentExpression);
}
}
static T Single<T>(this T[] array)
{
Verify.NotNull(array); // paramName: "array"
Verify.Argument(array.Length == 1, "Array must contain a single element."); // paramName: "array.Length == 1"
return array[0];
}View on GitHub (pinned to 05eb4800fc)
Solutions
- Confirm the value is within `[low, high]` before the call and branch on the overflow with a domain-specific message.
- Clamp with `Math.Clamp(argument, low, high)` when truncation to the ceiling is acceptable behavior.
- Raise or correctly compute `high` (e.g. `collection.Count - 1`) if the bound itself was wrong.
- Fix the source of the value so out-of-range quantities are rejected at the trust boundary.
Example fix
// before Guard.InRange(index, 0, list.Count); // after int safeIndex = Math.Clamp(index, 0, list.Count - 1); Guard.InRange(safeIndex, 0, list.Count - 1);
Defensive patterns
Strategy: validation
Validate before calling
static int Bounded(int value, int low, int high)
{
if (value > high) throw new ArgumentOutOfRangeException(nameof(value), $"{value} > {high}");
return value;
} Type guard
static bool IsAtMost(int value, int high) => value <= high;
Try / catch
try { Guard.InRange(value, low, high); }
catch (ArgumentOutOfRangeException ex) when (value > high)
{
// truncate to high or propagate a domain-specific error
} Prevention
- Compute upper bounds from real sizes (`count - 1`) rather than magic numbers.
- Clamp externally sourced values before they enter bounded APIs.
- Add boundary tests at exactly `high` and `high + 1`.
When it happens
Trigger: Calling `InRange(x, low, high)` with `x > high`. Supplying a size, index, or quantity that exceeds a hard cap (buffer length, column count, max items).
Common situations: Page numbers / page sizes above the configured maximum; array offsets computed without subtracting the length; values read from external services or config that exceed local limits; integer math that wraps or overflows an upper bound.
Related errors
- {argumentExpression} ({argument}) cannot be less than {lowEx
- Index must not be negative.
- Array must contain a single element.
- Empty names not allowed
AI-assisted analysis of dotnet/csharplang@05eb4800fc (2026-08-13).
Data as JSON: /api/errors/18ce09b1f90f4a0b.
Report an issue: GitHub.