Is there c# equivalent for comapring if one value exists in a list of values

Does c# have something like

if ( i == {1, 3, 5}
    // do something

do something would execute if i has one of these 3 values (1, 3, 5)

LVL 18
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Praveen KumarArchitectCommented:
how about..

case 1:
case 3:
case 5:
Console.WriteLine("Its 1, 2 or 3");

Not to my knowledge, the closet I can get is this.  You might find some use in C# 3.0 and its anonymous types, altered ofcourse.


            if (new int[] { 1, 2, 3 }.Contains(2))

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Priest04Author Commented:

I asked for if equivalent. since switch use only constants in case syntax. In this example it can be used, but in many  it cant, so the reason why I asked for if version.

I have searched on google and have found nothing equivalent, too, but you never know, eh? :) Thanks for the tip for 3.x framework version, I am still on 2.0, but it worth knowing when uograding.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.