Add and use ArgumentOutOfRangeException.ThrowIfNotBetweenOrEqual

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-25 16:54:54 +01:00
1 親 56031f443a
コミット cd19e8cdc1
12ファイル変更
+139 -98

検索条件に一致するファイルはありません

+41
ファイルの表示
@@ -55,6 +55,40 @@ internal static class ArgumentOutOfRangeExceptionExtensions
}
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not in the specified range.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="value">The argument to validate as non-zero or non-negative.</param>
/// <param name="minimum">The inclusive starting value for the range.</param>
/// <param name="maximum">The inclusive ending value for the range.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
[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);
}
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not in the specified range.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="value">The argument to validate as non-zero or non-negative.</param>
/// <param name="minimum">The inclusive starting value for the range.</param>
/// <param name="maximum">The inclusive ending value for the range.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
[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);
}
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> for a given parameter name.
/// </summary>
@@ -66,4 +100,11 @@ internal static class ArgumentOutOfRangeExceptionExtensions
{
throw new ArgumentOutOfRangeException(parameterName, value, null);
}
/// <inheritdoc cref="Throw(string?, int)"/>
[DoesNotReturn]
private static void Throw(string? parameterName, long value)
{
throw new ArgumentOutOfRangeException(parameterName, value, null);
}
}