Comparison
Provides generic methods to work with comparable values.
using System;
using System.Runtime.CompilerServices;
namespace DotNext
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Comparison
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T>
{
if (((IComparable<T>)value).CompareTo(min) < 0)
return min;
if (((IComparable<T>)value).CompareTo(max) > 0)
return max;
return value;
}
public static bool IsBetween<T>(this T value, T left, T right, BoundType boundType = BoundType.Open) where T : IComparable<T>
{
int num = ((IComparable<T>)value).CompareTo(left);
int num2 = ((IComparable<T>)value).CompareTo(right);
if (num > 0 || (num == 0 && (boundType & BoundType.LeftClosed) != 0)) {
if (num2 >= 0) {
if (num2 == 0)
return (boundType & BoundType.RightClosed) != BoundType.Open;
return false;
}
return true;
}
return false;
}
}
}