MosbruckerCJ
MosbruckerCJ

Reputation: 31

Is it possible for an if statement to equal multiple possibilities C#?

is it possible for an if then statement to be equal to multiple possibilities?

for example

if(option1 == "a","b","c");

?

Upvotes: 2

Views: 1934

Answers (3)

Giovanni Far
Giovanni Far

Reputation: 1653

if u want that option1 have to be 1 or 2 or 3 you have to do this:

if (option1 == 1 || option1 == 2 || option1 == 3)

Upvotes: 0

TheEvilPenguin
TheEvilPenguin

Reputation: 5682

Yes, you can do it one of two ways. You can check each case separately with the boolean or || operator:

if (option1 == "a" || option1 == "b" || etc...)

or you can create a collection of choices and see if it contains the variable you're testing:

if (new string[] { "a", "b", "c" }.Contains(option1))

Upvotes: 4

mikey
mikey

Reputation: 5160

I often wish for the same thing, this is what I do sometimes:

string[] optionOneVals = new string[3] {"a", "b", "c"};

if (optionOneVals.Contains(option1)) {
  ...
}

Upvotes: 5

Related Questions