using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace System; /// /// Throw helper extensions for . /// internal static class ArgumentOutOfRangeExceptionExtensions { /// /// Throws an if is negative. /// /// Dummy value to invoke the extension upon (always pass . /// The argument to validate as non-negative. /// The name of the parameter with which corresponds. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ThrowIfNegative(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { if (value < 0) { Throw(parameterName, value); } } /// /// Throws an if is negative or zero. /// /// Dummy value to invoke the extension upon (always pass . /// The argument to validate as non-zero or non-negative. /// The name of the parameter with which corresponds. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ThrowIfNegativeOrZero(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { if (value <= 0) { Throw(parameterName, value); } } /// /// Throws an if is greater than . /// /// Dummy value to invoke the extension upon (always pass . /// The argument to validate as less or equal than . /// The value to compare with . /// The name of the parameter with which corresponds. public static void ThrowIfGreaterThan(this ArgumentOutOfRangeException? _, int value, int other, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { if (value > other) { Throw(parameterName, value); } } /// /// Throws an if is not in the specified range. /// /// Dummy value to invoke the extension upon (always pass . /// The argument to validate as non-zero or non-negative. /// The inclusive starting value for the range. /// The exclusive ending value for the range. /// The name of the parameter with which corresponds. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ThrowIfNotInRange(this ArgumentOutOfRangeException? _, int value, int minimum, int maximum, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { if (value < minimum || value >= maximum) { Throw(parameterName, value); } } /// /// Throws an if is not in the specified range. /// /// Dummy value to invoke the extension upon (always pass . /// The argument to validate as non-zero or non-negative. /// The inclusive starting value for the range. /// The inclusive ending value for the range. /// The name of the parameter with which corresponds. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ThrowIfNotBetweenOrEqual(this ArgumentOutOfRangeException? _, int value, int minimum, int maximum, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { if (value < minimum || value > maximum) { Throw(parameterName, value); } } /// /// Throws an if is not in the specified range. /// /// Dummy value to invoke the extension upon (always pass . /// The argument to validate as non-zero or non-negative. /// The inclusive starting value for the range. /// The inclusive ending value for the range. /// The name of the parameter with which corresponds. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ThrowIfNotBetweenOrEqual(this ArgumentOutOfRangeException? _, long value, long minimum, long maximum, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { if (value < minimum || value > maximum) { Throw(parameterName, value); } } /// /// Throws an for a given parameter name. /// /// The name of the parameter to report in the exception. /// The invalid value that was used. /// Thrown with . [DoesNotReturn] private static void Throw(string? parameterName, int value) { throw new ArgumentOutOfRangeException(parameterName, value, null); } /// [DoesNotReturn] private static void Throw(string? parameterName, long value) { throw new ArgumentOutOfRangeException(parameterName, value, null); } }