Combining multiple conditional expressions in C#

前端 未结 9 1036
一个人的身影
一个人的身影 2021-01-30 11:24

In C#, instead of doing if(index == 7 || index == 8), is there a way to combine them? I\'m thinking of something like if(index == (7, 8)).

相关标签:
9条回答
  • 2021-01-30 12:11

    You could use this:

     if (new List<int>() { 7, 8 }.Contains(index))
    
    0 讨论(0)
  • 2021-01-30 12:12

    There is no way, in the current C# syntax set, to combine multiple right-hand-side operands to be passed to a single binary operator to the best of my knowledge.

    0 讨论(0)
  • 2021-01-30 12:18

    No way to do that, but you could certainly do a range using if( index >=7 && index <= 8 ). But giving it a list of numbers would require you to create an array or list object and then use a method to do this. But that's just overkill.

    0 讨论(0)
提交回复
热议问题