在IF子句中OR'ing多个值选项的替代方法是什么?

Joe*_*man 3 c# if-statement

是否有任何c#语法可以使if语句更清晰/更短?

if (token == "(" || token == ")" || token == "+" || token == "-" || token == "*" || token == "/")
{
     //do something
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*gen 10

像这样:

// 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
}
Run Code Online (Sandbox Code Playgroud)

或者使用数组:( 这样,您可以在匹配中的多个字符上进行验证.(此示例中未包括))

// 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
}
Run Code Online (Sandbox Code Playgroud)