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

前端 未结 11 1632
无人及你
无人及你 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 09:56

    I often use an extension method that mimics SQLs IN:

    public static bool IsIn(this T obj, params T[] collection) {
       return collection.Contains(obj);
    }
    

    That way I can do

    if(a.IsIn(b, c, d)) { ... }
    

提交回复
热议问题