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

Posted on 2008-02-06
Medium Priority
Last Modified: 2010-04-15
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)

Question by:Priest04
LVL 12

Expert Comment

by:Praveen Kumar
ID: 20832209
how about..

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

LVL 20

Accepted Solution

REA_ANDREW earned 2000 total points
ID: 20832252
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

LVL 18

Author Comment

ID: 20840495

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.


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month3 days, 13 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question