////// Range Helper /// ///public class RangeHelper where T : IComparable { /// /// 为val返回指定范围内合法的值 /// /// /// /// ///public static T ValidateRange(T val, T min, T max) { val = val.CompareTo(min) < 0 ? min : val; val = val.CompareTo(max) > 0 ? max : val; return val; } }
eg.
CurrentPage = RangeHelper .ValidateRange(CurrentPage, 1, TotalPage);
IComparable.CompareTo 方法用法说明
https://msdn.microsoft.com/zh-cn/library/system.icomparable.compareto.aspx