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

后端 未结 7 1204

Is there a shorter way of writing something like this:

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

Wha

7条回答
  •  独厮守ぢ
    2020-12-08 19:05

    public static bool In(this T x, params T[] set)
    {
        return set.Contains(x);
    }
    
    ...
    
    if (x.In(1, 2, 3)) 
    { ... }
    

    Required reading: MSDN Extension methods

提交回复
热议问题