RangeCheck Class

Provides methods to perform range checks and for constraining values to be within ranges.

public static class RangeCheck

Methods

NameDescription
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
value T
The value being clamped.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
Returns

T If value < min, min. If value > max, max. Otherwise, value.

Exceptions
ArgumentException
min is greater than max.

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 : notnull
Type parameters
T
The type of the values being compared.
Parameters
value T
The value being clamped.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
comparer IComparer<T>
An IComparer<T> to use for comparisons.
Returns

T If value < min, min. If value > max, max. Otherwise, value.

Exceptions
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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
value T?
The value being clamped.
min T
The inclusive lower bound of the range.
max T
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
ArgumentException
min is greater than 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.

public static T? ClampOrNull<T>(T? value, T min, T max, IComparer<T> comparer) where T : struct
Type parameters
T
The type of the values being compared.
Parameters
value T?
The value being clamped.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
comparer IComparer<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
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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
value T?
The value being clamped.
min T
The inclusive lower bound of the range.
max T
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
ArgumentException
min is greater than 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.

public static T? ClampOrNull<T>(T? value, T min, T max, IComparer<T> comparer) where T : class
Type parameters
T
The type of the values being compared.
Parameters
value T?
The value being clamped.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
comparer IComparer<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
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
defaultValue T?
The value to return if value is not within the specified range.
acceptNull bool
true to consider null values as if within range; false otherwise.
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
ArgumentException
min is greater than max.

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 : struct
Type parameters
T
The type of the values being compared.
Parameters
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
defaultValue T?
The value to return if value is not within the specified range.
acceptNull bool
true to consider null values as if within range; false otherwise.
comparer IComparer<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
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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
value T
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
defaultValue T
The value to return if value is not within the specified range.
Returns

T If value is within min and max, including boundaries, value; otherwise, defaultValue.

Exceptions
ArgumentException
min is greater than max.

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
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
defaultValue T?
The value to return if value is not within the specified range.
acceptNull bool
true to consider null values as if within range; false otherwise.
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
ArgumentException
min is greater than max.

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 : class
Type parameters
T
The type of the values being compared.
Parameters
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
defaultValue T?
The value to return if value is not within the specified range.
acceptNull bool
true to consider null values as if within range; false otherwise.
comparer IComparer<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
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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 : notnull
Type parameters
T
The type of the values being compared.
Parameters
value T
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
defaultValue T
The value to return if value is not within the specified range.
comparer IComparer<T>
The comparer to use for comparisons.
Returns

T If value is within min and max, including boundaries, value; otherwise, defaultValue.

Exceptions
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
acceptNull bool
The Boolean value to return when value is null.
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
ArgumentException
min is greater than max.

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 : struct
Type parameters
T
The type of the values being compared.
Parameters
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
acceptNull bool
The Boolean value to return when value is null.
comparer IComparer<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
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

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
value T
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
Returns

bool true if value is within min and max, including boundaries; otherwise, false.

Exceptions
ArgumentException
min is greater than max.

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
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
acceptNull bool
The Boolean value to return when value is null.
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
ArgumentException
min is greater than max.

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 : class
Type parameters
T
The type of the values being compared.
Parameters
value T?
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
acceptNull bool
The Boolean value to return when value is null.
comparer IComparer<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
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.

Verify<T>(T, T, T, IComparer<T>)

Checks whether a value is within an inclusive range, using the specified comparer.

public static bool Verify<T>(T value, T min, T max, IComparer<T> comparer) where T : notnull
Type parameters
T
The type of the values being compared.
Parameters
value T
The value being checked.
min T
The inclusive lower bound of the range.
max T
The inclusive upper bound of the range.
comparer IComparer<T>
The comparer to use for comparisons.
Returns

bool true if value is within min and max, including boundaries; otherwise, false.

Exceptions
ArgumentNullException
comparer is null.
ArgumentException
min is greater than max.