【问题标题】:Test whether an integer is between two other integers测试一个整数是否在其他两个整数之间
【发布时间】:2017-03-31 13:24:08
【问题描述】:

我搜索以测试一个整数是否介于其他两个整数之间,而不使用“if”条件,但使用 c# 中的特定方法。

有可能吗?

感谢您的帮助

【问题讨论】:

    标签: c# integer


    【解决方案1】:

    您可以使用扩展方法。

    你必须创建一个静态类:

    namespace Extension
    { 
    static class MyIntMethods
      {
        public static bool Between(this int value,int min, int max)
        {
            return (value > min && value < max) ? true : false;
        }
      }
    }
    

    下一步: 在要使用新方法的类中添加命名空间:

    using Extension;
    

    现在使用新方法:

    int n1 = 1;
    n2 = 2;
    n3 = 3;
    
    result = n2.Between(n1, n3);
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 2016-10-12
      • 2012-05-27
      • 2016-01-18
      • 1970-01-01
      相关资源
      最近更新 更多