• 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 }
  • 相关阅读:
    Django 模型层
    Django 模板层
    就在明晚,3月10日19:30,#HMS Core Insights#直播来了!
    Unity平台 | 快速集成华为性能管理服务
    手把手教你集成华为机器学习服务(ML Kit)人脸检测功能
    在onBackPress中实现退出拦截时不生效
    在家也可以享受购物体验?AR+Scene 帮您实现商品3D展示和虚拟试戴
    web组件发消息给H5页面时报错
    借助近距离通信,实现手机作为游戏控制器掌控大屏游戏
    快应用审核常见问题
  • 原文地址:https://www.cnblogs.com/oceanho/p/4883192.html
Copyright © 2020-2023  润新知