Check if a variable is in an ad-hoc list of values

后端 未结 7 1186

Is there a shorter way of writing something like this:

if(x==1 || x==2 || x==3) // do something

Wha

7条回答
  •  囚心锁ツ
    2020-12-08 18:56

    You could achieve this by using the List.Contains method:

    if(new []{1, 2, 3}.Contains(x))
    {
        //x is either 1 or 2 or 3
    }
    

提交回复
热议问题