有没有办法让比较更短

and*_*ewc 3 c# comparison tuples

我怎样才能缩短这个时间?

string veryLongVariableName;

if (veryLongVariableName == "a" || veryLongVariableName == "b" || veryLongVariableName == "c"|| veryLongVariableName == "d"|| veryLongVariableName == "e"|| veryLongVariableName == "f")
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

if (veryLongVariableName == ("a", "b", "c", "d", "e", "f"))
if (veryLongVariableName == ("a" || "b" || "c" || "d" || "e" || "f"))
Run Code Online (Sandbox Code Playgroud)

AAA*_*ddd 6

你几乎拥有它

if (new [] {"a", "b", "c", "d", "e", "f"}.Contains(veryLongVariableName))
Run Code Online (Sandbox Code Playgroud)

注意:每次调用它时都会分配

  • 注意,可以通过在某处将其创建为静态数组来避免分配;但是……这有优点也有缺点 (2认同)