Home > DeveloperSection > Forums > Check if a variable is in an ad-hoc list of values
lois waisbrooker
lois waisbrooker

Total Post:70

Posted on    June-06-2013 3:22 AM

 C# C# 

 1 Reply(s)
 1088  View(s)
Rate this:
Hi Expert,

Is there a shorter way of writing something like this:

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

What I'm looking for is something like this:

if(x.in((1,2,3)) // do something

Please help.
Thanks in advance.


Total Post:604

Posted on    June-06-2013 8:32 AM

Hi Expert,

You could achieve this by using the List.Contains method:

if(new []{1, 2, 3}.Contains(x))
    //x is either 1 or 2 or 3

I hope it helpful for you.

Don't want to miss updates? Please click the below button!

Follow MindStick