测试整数是否在两个其他整数之间

时间:2016-11-17 05:44:06

标签: c# integer

我搜索测试一个整数是否在两个其他整数之间而不使用if条件但是使用特定方法。

有可能吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用扩展方法。

您必须创建一个静态类:

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);
相关问题