C#. Do if( a == (b or c or d)). Is it possible?

前端 未结 11 1765
无人及你
无人及你 2021-02-02 09:31

Is there another way to write something like this:

if (a == x || a == y || a == z)

One way that I found is doing it like this:

         


        
11条回答
  •  旧巷少年郎
    2021-02-02 10:09

    Your solution to rewrite it as

    if( new [] {x,y,z}.Contains(a))
    

    is not a good move.

    You've take a simple efficient logical operation, which every programmer easily understands and which contains short-circuiting logic to speed it up and instead you've produced code that requires a moment to understand and which is considerably less efficient.

    Sometimes your fellow engineers will prefer it if you don't try to be "clever"!

提交回复
热议问题