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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -