c# equivalent to pythons 'a in [1, 2, 3]' -


is there equivalent python's a in [1, 2, 3] in c#?

i've tried new int[]{1, 2, 3}.contains(a) -- lot uglier, same.

of course, do, (a==1 || a==2 || a==3) different variables , namespaces, can bit clunky too.

just throwing out there , untested.

public static bool in(this object obj, params object[] items) {     return items.any(o => o.equals(obj)); } 

i think extension method looking , tweak actual comparison check suit needs. went object work anything, can more specific if you'd like.

just call so:

if (4.in(1,2,3,4)) {     // } 

you change take in ienumerable, if have 1 best call .contains() or .any() on collection.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -