CHECK IF A VARIABLE IS IN AN AD-HOC LIST OF VALUES

lois waisbrooker

Total Post:70

Points:494
Posted by  lois waisbrooker
C# 
C#
 1122  View(s)
Ratings:
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.
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Check if a variable is in an ad-hoc list of values

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!