【发布时间】:2017-03-31 13:24:08
【问题描述】:
我搜索以测试一个整数是否介于其他两个整数之间,而不使用“if”条件,但使用 c# 中的特定方法。
有可能吗?
感谢您的帮助
【问题讨论】:
我搜索以测试一个整数是否介于其他两个整数之间,而不使用“if”条件,但使用 c# 中的特定方法。
有可能吗?
感谢您的帮助
【问题讨论】:
您可以使用扩展方法。
你必须创建一个静态类:
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);
【讨论】: