是否有任何 C# 语法可以使此 if 语句更清晰/更短?
if (token == "(" || token == ")" || token == "+" || token == "-" || token == "*" || token == "/")
{
//do something
}
最佳答案
像这样:
// create a string with the valid chars
var tokens = "()+-*/";
// this will call the Contains method of the String class
if(tokens.Contains(token))
{
//do something
}
或者使用数组:(这样您就可以在一个匹配项中验证多个字符。(不包括在此示例中))
// create an array with the valid strings
var tokens = new [] { "(", ")", "+", "-", "*", "/" };
// this will call Contains method of the Enumerable class
if(tokens.Contains(token))
{
//do something
}
关于c# - 在 IF 子句中 OR'ing 多个值选项的替代方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39568660/