-
Notifications
You must be signed in to change notification settings - Fork 5.3k
ArgOutOfRangeException throw helpers #78222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
661eef8
1900b3a
39e6e81
600b947
91e279e
541389f
ad4aff8
722f5b7
3c5fe6d
65d5d23
9f81c22
0709d8c
0067cbd
b760a77
6893695
b65e7d5
dd0ad2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ | |
| =============================================================================*/ | ||
|
|
||
| using System.Runtime.Serialization; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Numerics; | ||
|
|
||
| namespace System | ||
| { | ||
|
|
@@ -85,5 +88,85 @@ public override string Message | |
|
|
||
| // Gets the value of the argument that caused the exception. | ||
| public virtual object? ActualValue => _actualValue; | ||
|
|
||
| [DoesNotReturn] | ||
| private static void Throw(string? paramName, string message) | ||
| { | ||
| throw new ArgumentOutOfRangeException(paramName, message); | ||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is zero.</summary> | ||
| /// <param name="value">The argument to validate as non-zero.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfZero<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : INumberBase<T> | ||
| { | ||
| if (T.IsZero(value)) | ||
| Throw(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName)); | ||
hrrrrustic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary> | ||
| /// <param name="value">The argument to validate as non-negative.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfNegative<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : INumberBase<T> | ||
| { | ||
| if (T.IsNegative(value)) | ||
| Throw(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName)); | ||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative or zero.</summary> | ||
| /// <param name="value">The argument to validate as non-zero or non-negative.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : INumberBase<T> | ||
| { | ||
| ThrowIfNegative(value, paramName); | ||
| ThrowIfZero(value, paramName); | ||
|
||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than <paramref name="other"/>.</summary> | ||
| /// <param name="value">The argument to validate as less or equal than <paramref name="other"/>.</param> | ||
| /// <param name="other">The value to compare with <paramref name="value"/>.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfGreaterThan<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : IComparable<T> | ||
| { | ||
| if (value.CompareTo(other) == 1) | ||
hrrrrustic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Throw(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLowerOrEqual, paramName, other)); | ||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than or equal <paramref name="other"/>.</summary> | ||
| /// <param name="value">The argument to validate as less than <paramref name="other"/>.</param> | ||
| /// <param name="other">The value to compare with <paramref name="value"/>.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : IComparable<T> | ||
| { | ||
| if (value.CompareTo(other) != -1) | ||
hrrrrustic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Throw(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLower, paramName, other)); | ||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than <paramref name="other"/>.</summary> | ||
| /// <param name="value">The argument to validate as greatar than or equal than <paramref name="other"/>.</param> | ||
| /// <param name="other">The value to compare with <paramref name="value"/>.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfLessThan<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : IComparable<T> | ||
| { | ||
| if (value.CompareTo(other) == -1) | ||
hrrrrustic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Throw(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other)); | ||
| } | ||
|
|
||
| /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than or equal <paramref name="other"/>.</summary> | ||
| /// <param name="value">The argument to validate as greatar than than <paramref name="other"/>.</param> | ||
| /// <param name="other">The value to compare with <paramref name="value"/>.</param> | ||
| /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
| public static void ThrowIfLessThanOrEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) | ||
| where T : IComparable<T> | ||
| { | ||
| if (value.CompareTo(other) != 1) | ||
hrrrrustic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Throw(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other)); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.