C# RangeHelper

    /// <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

C# RangeHelper

上一篇:窗口类(Window Class)概述


下一篇:由window.history.back()引发的问题