CSharp任何可比较的数据类型(大小比较泛型实现方法)封装
1 /// <summary> 2 /// 判定A等于B(A.CompareTo(B)==0) 3 /// </summary> 4 /// <typeparam name="T">实现了 IComparable 接口的任意对象 </typeparam> 5 /// <param name="a">比较对象A</param> 6 /// <param name="b">比较对象B</param> 7 /// <returns></returns> 8 private static bool A_EQ_B<T>(T a, T b) where T : IComparable 9 { 10 return a.CompareTo(b)== 0; 11 } 12 13 /// <summary> 14 /// 判定A大于B(A.CompareTo(B)<0) 15 /// </summary> 16 /// <typeparam name="T">实现了 IComparable 接口的任意对象 </typeparam> 17 /// <param name="a">比较对象A</param> 18 /// <param name="b">比较对象B</param> 19 /// <returns></returns> 20 private static bool A_GT_B<T>(T a, T b) where T : IComparable 21 { 22 return a.CompareTo(b) > 0; 23 } 24 25 /// <summary> 26 /// 判定A大于或等于B(A.CompareTo(B)<=0) 27 /// </summary> 28 /// <typeparam name="T">实现了 IComparable 接口的任意对象 </typeparam> 29 /// <param name="a">比较对象A</param> 30 /// <param name="b">比较对象B</param> 31 /// <returns></returns> 32 private static bool A_GET_B<T>(T a, T b) where T : IComparable 33 { 34 return a.CompareTo(b) >= 0; 35 } 36 37 /// <summary> 38 /// 判定A小于B(A.CompareTo(B)>0) 39 /// </summary> 40 /// <typeparam name="T">实现了 IComparable 接口的任意对象 </typeparam> 41 /// <param name="a">比较对象A</param> 42 /// <param name="b">比较对象B</param> 43 /// <returns></returns> 44 private static bool A_LT_B<T>(T a, T b) where T : IComparable 45 { 46 return a.CompareTo(b) < 0; 47 } 48 49 /// <summary> 50 /// 判定A小于或等于B(A.CompareTo(B)>=0) 51 /// </summary> 52 /// <typeparam name="T">实现了 IComparable 接口的任意对象 </typeparam> 53 /// <param name="a">比较对象A</param> 54 /// <param name="b">比较对象B</param> 55 /// <returns></returns> 56 private static bool A_LET_B<T>(T a, T b) where T : IComparable 57 { 58 return a.CompareTo(b) <= 0; 59 }