Class ComparableExtensions
- Namespace
- Plugin.BaseTypeExtensions
- Assembly
- Plugin.BaseTypeExtensions.dll
Provides extension methods for types implementing IComparable<T> for range checking and comparison operations.
public static class ComparableExtensions
- Inheritance
-
ComparableExtensions
- Inherited Members
Methods
IsBetween<T>(T, T, T, bool)
Checks if a comparable value is between two bounds. Uses Min<T>(T, T) and Max<T>(T, T) to determine the actual min/max regardless of parameter order. Then uses the comparison extension methods for readable and consistent logic.
public static bool IsBetween<T>(this T value, T bound1, T bound2, bool inclusive = true) where T : IComparable<T>
Parameters
value
TThe value to check
bound1
TFirst boundary value (can be min or max)
bound2
TSecond boundary value (can be min or max)
inclusive
boolWhether to include the boundaries in the range (default: true)
Returns
- bool
True if the value is between the bounds; false otherwise
Type Parameters
T
Any type that implements IComparable<T>
Examples
var isInRange = 5.IsBetween(1, 10); // true (inclusive by default)
var isInRange = 5.IsBetween(10, 1); // true (order doesn't matter)
var isInRange = 5.IsBetween(1, 5, false); // false (exclusive)
var isInRange = 5.IsBetween(1, 5, true); // true (inclusive)
IsGreaterThanOrEqual<T>(T, T)
Checks if the current value is greater than or equal to another comparable value.
Provides a more readable alternative to value.CompareTo(other) >= 0
.
public static bool IsGreaterThanOrEqual<T>(this T value, T other) where T : IComparable<T>
Parameters
value
TThe value to compare
other
TThe value to compare against
Returns
- bool
True if
value
is greater than or equal toother
; false otherwise
Type Parameters
T
Any type that implements IComparable<T>
Examples
var isGreaterOrEqual = 10.IsGreaterThanOrEqual(5); // true
var isGreaterOrEqual = 5.IsGreaterThanOrEqual(5); // true (equal)
var isGreaterOrEqual = 3.IsGreaterThanOrEqual(7); // false
IsGreaterThan<T>(T, T)
Checks if the current value is greater than another comparable value.
Provides a more readable alternative to value.CompareTo(other) > 0
.
public static bool IsGreaterThan<T>(this T value, T other) where T : IComparable<T>
Parameters
value
TThe value to compare
other
TThe value to compare against
Returns
- bool
True if
value
is greater thanother
; false otherwise
Type Parameters
T
Any type that implements IComparable<T>
Examples
var isGreater = 10.IsGreaterThan(5); // true
var isGreater = 3.IsGreaterThan(7); // false
var isGreater = 5.IsGreaterThan(5); // false (not greater, equal)
IsLessThanOrEqual<T>(T, T)
Checks if the current value is less than or equal to another comparable value.
Provides a more readable alternative to value.CompareTo(other) <= 0
.
public static bool IsLessThanOrEqual<T>(this T value, T other) where T : IComparable<T>
Parameters
value
TThe value to compare
other
TThe value to compare against
Returns
- bool
True if
value
is less than or equal toother
; false otherwise
Type Parameters
T
Any type that implements IComparable<T>
Examples
var isLessOrEqual = 3.IsLessThanOrEqual(7); // true
var isLessOrEqual = 5.IsLessThanOrEqual(5); // true (equal)
var isLessOrEqual = 10.IsLessThanOrEqual(5); // false
IsLessThan<T>(T, T)
Checks if the current value is less than another comparable value.
Provides a more readable alternative to value.CompareTo(other) < 0
.
public static bool IsLessThan<T>(this T value, T other) where T : IComparable<T>
Parameters
value
TThe value to compare
other
TThe value to compare against
Returns
- bool
True if
value
is less thanother
; false otherwise
Type Parameters
T
Any type that implements IComparable<T>
Examples
var isLess = 3.IsLessThan(7); // true
var isLess = 10.IsLessThan(5); // false
var isLess = 5.IsLessThan(5); // false (not less, equal)