/// <summary> /// Range Helper /// </summary> /// <typeparam name="T"></typeparam> public class RangeHelper<T> where T : IComparable<T> { /// <summary> /// 为val返回指定范围内合法的值 /// </summary> /// <param name="val"></param> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> 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<int>.ValidateRange(CurrentPage, 1, TotalPage);
IComparable.CompareTo 方法用法说明
https://msdn.microsoft.com/zh-cn/library/system.icomparable.compareto.aspx