如何结合||在条件语句运营商 [英] How to combine || operators in condition statement
本文介绍了如何结合||在条件语句运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if (foo == "1" || foo == "5" || foo == "9" ... )
我想将它们结合起来类似于以下(不工作):
I like to combine them similar to the following (which doesn't work):
if (foo == ("1" || "5" || "9" ... ))
这可能吗?
推荐答案
不幸的是没有,你最好的选择是创建一个扩展方法
Unfortunately not, your best bet is to create an extension method
public static bool IsOneOf<T>(this T value, params T[] options)
{
return options.Contains(value);
}
和您可以使用它是这样的:
and you can use it like this:
if (foo.IsOneOf("1", "5", "9"))
{
...
}
依据通用的,它可以用于任何类型(整型,字符串等)。
Being generic, it can be used for any type (int, string etc).
这篇关于如何结合||在条件语句运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文