RangeCheck Class
Provides methods to perform range checks and for constraining values to be within ranges.
public static class RangeCheckMethods
| Name | Description |
|---|---|
| Clamp<T>(T, T, T) | Returns value clamped to the inclusive range of min and max. |
| Clamp<T>(T, T, T, IComparer<T>) | Returns value clamped to the inclusive range of min and max, using the specified comparer. |
| ClampOrNull<T>(T?, T, T) | Returns value, if it is not null, clamped to the inclusive range of min and max. |
| ClampOrNull<T>(T?, T, T, IComparer<T>) | Returns value, if it is not null, clamped to the inclusive range of min and max, using the specified comparer. |
| ClampOrNull<T>(T?, T, T) | Returns value, if it is not null, clamped to the inclusive range of min and max. |
| ClampOrNull<T>(T?, T, T, IComparer<T>) | Returns value, if it is not null, clamped to the inclusive range of min and max, using the specified comparer. |
| InRangeOrDefault<T>(T?, T, T, T?, bool) | Returns a value if it is within an inclusive range, or a default value otherwise. |
| InRangeOrDefault<T>(T?, T, T, T?, bool, IComparer<T>) | Returns a value if it is within an inclusive range, or a default value otherwise, using a specified comparer. |
| InRangeOrDefault<T>(T, T, T, T) | Returns a value if it is within an inclusive range, or a default value otherwise. |
| InRangeOrDefault<T>(T?, T, T, T?, bool) | Returns a value if it is within an inclusive range, or a default value otherwise. |
| InRangeOrDefault<T>(T?, T, T, T?, bool, IComparer<T>) | Returns a value if it is within an inclusive range, or a default value otherwise, using a specified comparer. |
| InRangeOrDefault<T>(T, T, T, T, IComparer<T>) | Returns a value if it is within an inclusive range, or a default value otherwise, using a specified comparer. |
| Verify<T>(T?, T, T, bool) | Checks whether a nullable value is within an inclusive range, accepting or rejecting null values according to the value of the acceptNull parameter. |
| Verify<T>(T?, T, T, bool, IComparer<T>) | Checks whether a nullable value is within an inclusive range, using the specified comparer, and accepting or rejecting null values according to the value of the acceptNull parameter. |
| Verify<T>(T, T, T) | Checks whether a value is within an inclusive range. |
| Verify<T>(T?, T, T, bool) | Checks whether a nullable value is within an inclusive range, accepting or rejecting null values according to the value of the acceptNull parameter. |
| Verify<T>(T?, T, T, bool, IComparer<T>) | Checks whether a nullable value is within an inclusive range, using the specified comparer, and accepting or rejecting null values according to the value of the acceptNull parameter. |
| Verify<T>(T, T, T, IComparer<T>) | Checks whether a value is within an inclusive range, using the specified comparer. |
Clamp overloads
Clamp<T>(T, T, T)
Returns value clamped to the inclusive range of min and max.
public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT- The value being clamped.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
Returns
T If value < min, min. If value > max, max. Otherwise, value.
Exceptions
ArgumentExceptionminis greater thanmax.
Clamp<T>(T, T, T, IComparer<T>)
Returns value clamped to the inclusive range of min and max, using the specified comparer.
public static T Clamp<T>(T value, T min, T max, IComparer<T> comparer) where T : notnullType parameters
T- The type of the values being compared.
Parameters
valueT- The value being clamped.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
comparerIComparer<T>- An IComparer<T> to use for comparisons.
Returns
T If value < min, min. If value > max, max. Otherwise, value.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
ClampOrNull overloads
ClampOrNull<T>(T?, T, T)
Returns value, if it is not null, clamped to the inclusive range of min and max.
public static T? ClampOrNull<T>(T? value, T min, T max) where T : struct, IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT?- The value being clamped.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
Returns
T? If value is null, null. If value < min, min. If value > max, max. Otherwise, value.
Exceptions
ArgumentExceptionminis greater thanmax.
ClampOrNull<T>(T?, T, T, IComparer<T>)
Returns value, if it is not null, clamped to the inclusive range of min and max, using the specified comparer.
public static T? ClampOrNull<T>(T? value, T min, T max, IComparer<T> comparer) where T : structType parameters
T- The type of the values being compared.
Parameters
valueT?- The value being clamped.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
comparerIComparer<T>- An IComparer<T> to use for comparisons.
Returns
T? If value is null, null. If value < min, min. If value > max, max. Otherwise, value.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
ClampOrNull<T>(T?, T, T)
Returns value, if it is not null, clamped to the inclusive range of min and max.
public static T? ClampOrNull<T>(T? value, T min, T max) where T : class, IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT?- The value being clamped.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
Returns
T? If value is null, null. If value < min, min. If value > max, max. Otherwise, value.
Exceptions
ArgumentExceptionminis greater thanmax.
ClampOrNull<T>(T?, T, T, IComparer<T>)
Returns value, if it is not null, clamped to the inclusive range of min and max, using the specified comparer.
public static T? ClampOrNull<T>(T? value, T min, T max, IComparer<T> comparer) where T : classType parameters
T- The type of the values being compared.
Parameters
valueT?- The value being clamped.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
comparerIComparer<T>- An IComparer<T> to use for comparisons.
Returns
T? If value is null, null. If value < min, min. If value > max, max. Otherwise, value.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
InRangeOrDefault overloads
InRangeOrDefault<T>(T?, T, T, T?, bool)
Returns a value if it is within an inclusive range, or a default value otherwise.
public static T? InRangeOrDefault<T>(T? value, T min, T max, T? defaultValue, bool acceptNull) where T : struct, IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
defaultValueT?- The value to return if
valueis not within the specified range. acceptNullbooltrueto considernullvalues as if within range;falseotherwise.
Returns
T? If value is null and acceptNull is true, null. If value is null and acceptNull is false, defaultValue. If value is within min and max, including boundaries, value. Otherwise, defaultValue.
Exceptions
ArgumentExceptionminis greater thanmax.
InRangeOrDefault<T>(T?, T, T, T?, bool, IComparer<T>)
Returns a value if it is within an inclusive range, or a default value otherwise, using a specified comparer.
public static T? InRangeOrDefault<T>(T? value, T min, T max, T? defaultValue, bool acceptNull, IComparer<T> comparer) where T : structType parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
defaultValueT?- The value to return if
valueis not within the specified range. acceptNullbooltrueto considernullvalues as if within range;falseotherwise.comparerIComparer<T>- The comparer to use for comparisons.
Returns
T? If value is null and acceptNull is true, null. If value is null and acceptNull is false, defaultValue. If value is within min and max, including boundaries, value. Otherwise, defaultValue.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
InRangeOrDefault<T>(T, T, T, T)
Returns a value if it is within an inclusive range, or a default value otherwise.
public static T InRangeOrDefault<T>(T value, T min, T max, T defaultValue) where T : IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
defaultValueT- The value to return if
valueis not within the specified range.
Returns
T If value is within min and max, including boundaries, value; otherwise, defaultValue.
Exceptions
ArgumentExceptionminis greater thanmax.
InRangeOrDefault<T>(T?, T, T, T?, bool)
Returns a value if it is within an inclusive range, or a default value otherwise.
public static T? InRangeOrDefault<T>(T? value, T min, T max, T? defaultValue, bool acceptNull) where T : class, IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
defaultValueT?- The value to return if
valueis not within the specified range. acceptNullbooltrueto considernullvalues as if within range;falseotherwise.
Returns
T? If value is null and acceptNull is true, null. If value is null and acceptNull is false, defaultValue. If value is within min and max, including boundaries, value. Otherwise, defaultValue.
Exceptions
ArgumentExceptionminis greater thanmax.
InRangeOrDefault<T>(T?, T, T, T?, bool, IComparer<T>)
Returns a value if it is within an inclusive range, or a default value otherwise, using a specified comparer.
public static T? InRangeOrDefault<T>(T? value, T min, T max, T? defaultValue, bool acceptNull, IComparer<T> comparer) where T : classType parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
defaultValueT?- The value to return if
valueis not within the specified range. acceptNullbooltrueto considernullvalues as if within range;falseotherwise.comparerIComparer<T>- The comparer to use for comparisons.
Returns
T? If value is null and acceptNull is true, null. If value is null and acceptNull is false, defaultValue. If value is within min and max, including boundaries, value. Otherwise, defaultValue.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
InRangeOrDefault<T>(T, T, T, T, IComparer<T>)
Returns a value if it is within an inclusive range, or a default value otherwise, using a specified comparer.
public static T InRangeOrDefault<T>(T value, T min, T max, T defaultValue, IComparer<T> comparer) where T : notnullType parameters
T- The type of the values being compared.
Parameters
valueT- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
defaultValueT- The value to return if
valueis not within the specified range. comparerIComparer<T>- The comparer to use for comparisons.
Returns
T If value is within min and max, including boundaries, value; otherwise, defaultValue.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
Verify overloads
Verify<T>(T?, T, T, bool)
Checks whether a nullable value is within an inclusive range, accepting or rejecting null values according to the value of the acceptNull parameter.
public static bool Verify<T>(T? value, T min, T max, bool acceptNull) where T : struct, IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
acceptNullbool- The Boolean value to return when
valueisnull.
Returns
bool If value is not null: true if value is within min and max, including boundaries; otherwise, false. If value is null: the value passed in acceptNull.
Exceptions
ArgumentExceptionminis greater thanmax.
Verify<T>(T?, T, T, bool, IComparer<T>)
Checks whether a nullable value is within an inclusive range, using the specified comparer, and accepting or rejecting null values according to the value of the acceptNull parameter.
public static bool Verify<T>(T? value, T min, T max, bool acceptNull, IComparer<T> comparer) where T : structType parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
acceptNullbool- The Boolean value to return when
valueisnull. comparerIComparer<T>- The comparer to use for comparisons.
Returns
bool If value is not null: true if value is within min and max, including boundaries; otherwise, false. If value is null: the value passed in acceptNull.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
Verify<T>(T, T, T)
Checks whether a value is within an inclusive range.
public static bool Verify<T>(T value, T min, T max) where T : IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
Returns
bool true if value is within min and max, including boundaries; otherwise, false.
Exceptions
ArgumentExceptionminis greater thanmax.
Verify<T>(T?, T, T, bool)
Checks whether a nullable value is within an inclusive range, accepting or rejecting null values according to the value of the acceptNull parameter.
public static bool Verify<T>(T? value, T min, T max, bool acceptNull) where T : class, IComparable<T>Type parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
acceptNullbool- The Boolean value to return when
valueisnull.
Returns
bool If value is not null: true if value is within min and max, including boundaries; otherwise, false. If value is null: the value passed in acceptNull.
Exceptions
ArgumentExceptionminis greater thanmax.
Verify<T>(T?, T, T, bool, IComparer<T>)
Checks whether a nullable value is within an inclusive range, using the specified comparer, and accepting or rejecting null values according to the value of the acceptNull parameter.
public static bool Verify<T>(T? value, T min, T max, bool acceptNull, IComparer<T> comparer) where T : classType parameters
T- The type of the values being compared.
Parameters
valueT?- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
acceptNullbool- The Boolean value to return when
valueisnull. comparerIComparer<T>- The comparer to use for comparisons.
Returns
bool If value is not null: true if value is within min and max, including boundaries; otherwise, false. If value is null: the value passed in acceptNull.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.
Verify<T>(T, T, T, IComparer<T>)
Checks whether a value is within an inclusive range, using the specified comparer.
Type parameters
T- The type of the values being compared.
Parameters
valueT- The value being checked.
minT- The inclusive lower bound of the range.
maxT- The inclusive upper bound of the range.
comparerIComparer<T>- The comparer to use for comparisons.
Returns
bool true if value is within min and max, including boundaries; otherwise, false.
Exceptions
ArgumentNullExceptioncomparerisnull.ArgumentExceptionminis greater thanmax.