Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare multiple variables to a single condition?

I have this:

if (input.Text.ToUpper() == "STOP")

But there are so many possible values that I wouldn't be able to specify them all separately like this:

if (input.Text.ToUpper() == "STOP" || input.Text.ToUpper() == "END")

Is there a way that you can do something like this:

if (input.Text.ToUpper() == "STOP", "END", "NO", "YES")

So that using STOP, END, NO, or YES will do the task?

Using any contains won't work, other times accepted words will have the word STOP and END in them.

like image 441
Jon Avatar asked Nov 27 '22 21:11

Jon


1 Answers

You could use a collection like array and Enumerable.Contains:

var words = new[]{ "STOP", "END", "NO", "YES" };
if(words.Contains(input.Text.ToUpper()))
{
     // ...      
}
like image 88
Tim Schmelter Avatar answered Dec 15 '22 07:12

Tim Schmelter