Tag*_*gon 2 c# variables wpf int compare
是否存在可以替换if()参数的短代码,例如:
int x = 1; // x can be 1,2,3 etc.
if(x==1 || x==3 || x==12)
{
//do something..
}
Run Code Online (Sandbox Code Playgroud)
我不想重复x == 1,x == 3等,只是将数字与x进行比较.
您可以在数组中包含可能的数字,然后将其比较如下:
int x = 1;
int[] compareArray = new[] { 1, 3, 12, 33 };
if (compareArray.Contains(x))
{
//condition met
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用Enumerable.Any
:
if (compareArray.Any(r=> r == x))
Run Code Online (Sandbox Code Playgroud)