public static T Clamp<T>(T value, T max, T min)
where T : System.IComparable<T> {
T result = value;
if (value.CompareTo(max) > 0)
result = max;
if (value.CompareTo(min) < 0)
result = min;
return result;
}
它可以像这样使用:
int i = Clamp(12, 10, 0); -> i == 10
double d = Clamp(4.5, 10.0, 0.0); -> d == 4.5
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if(val.CompareTo(max) > 0) return max;
else return val;
}
扩展方法放在静态类中——因为这是一个相当低级的函数,所以它可能应该放在项目中的某个核心命名空间中。然后,您可以在包含名称空间的 using 指令的任何代码文件中使用该方法。
using Core.ExtensionMethods
int i = 4.Clamp(1, 3);
public static T Clamped<T>(this T value, T min, T max) where T : IComparable<T> {
if (value == null) throw new ArgumentNullException(nameof(value), "is null.");
if (min == null) throw new ArgumentNullException(nameof(min), "is null.");
if (max == null) throw new ArgumentNullException(nameof(max), "is null.");
//If min <= max, clamp
if (min.CompareTo(max) <= 0) return value.CompareTo(min) < 0 ? min : value.CompareTo(max) > 0 ? max : value;
//If min > max, clamp on swapped min and max
return value.CompareTo(max) < 0 ? max : value.CompareTo(min) > 0 ? min : value;
}
Using the previous answers, I condensed it down to the below code for my needs. This will also allow you to clamp a number only by its min or max.
public static class IComparableExtensions
{
public static T Clamped<T>(this T value, T min, T max)
where T : IComparable<T>
{
return value.CompareTo(min) < 0 ? min : value.ClampedMaximum(max);
}
public static T ClampedMinimum<T>(this T value, T min)
where T : IComparable<T>
{
return value.CompareTo(min) < 0 ? min : value;
}
public static T ClampedMaximum<T>(this T value, T max)
where T : IComparable<T>
{
return value.CompareTo(max) > 0 ? max : value;
}
}
I know: The code consists of five super-ugly conditional expression operators. The thing is, 很管用, and the tests below prove it. Feel free to add strictly unnecessary parentheses if you so desire.
public class RangeLimit<T> where T : IComparable<T>
{
public T Min { get; }
public T Max { get; }
public RangeLimit(T min, T max)
{
if (min.CompareTo(max) > 0)
throw new InvalidOperationException("invalid range");
Min = min;
Max = max;
}
public void Validate(T param)
{
if (param.CompareTo(Min) < 0 || param.CompareTo(Max) > 0)
throw new InvalidOperationException("invalid argument");
}
public T Clamp(T param) => param.CompareTo(Min) < 0 ? Min : param.CompareTo(Max) > 0 ? Max : param;
}
这个类适用于所有 IComparable的对象。我创建了一个具有一定范围的实例:
RangeLimit<int> range = new RangeLimit<int>(0, 100);
namespace App
{
/// <summary>
/// Miscellaneous utilities.
/// </summary>
public static class Util
{
/// <summary>
/// Clamp a value to the inclusive range [min, max].
/// </summary>
/// <remarks>
/// In newer versions of the .NET Framework, there is a System.Math.Clamp() method.
/// </remarks>
/// <typeparam name="T">The type of value.</typeparam>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped value.</returns>
public static T clamp<T>( T value, T min, T max ) where T : System.IComparable<T>
{
if ( value.CompareTo( max ) > 0 )
{
return max;
}
if ( value.CompareTo( min ) < 0 )
{
return min;
}
return value;
}
}
}