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)&lt;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)&lt;=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)&gt;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)&gt;=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 }
相關文章
相關標籤/搜索