How to combine || operators in condition statement

C#.Net

C# Problem Overview


Instead of

if (foo == "1" || foo == "5" || foo == "9" ... ) 

I like to combine them similar to the following (which doesn't work):

if (foo == ("1" || "5" || "9" ... ))

Is that possible?

C# Solutions


Solution 1 - C#

Unfortunately not, your best bet is to create an extension method

public static bool IsOneOf<T>(this T value, params T[] options)
{
    return options.Contains(value);
}

and you can use it like this:

if (foo.IsOneOf("1", "5", "9"))
{
    ...
}

Being generic, it can be used for any type (int, string etc).

Solution 2 - C#

You cannot do it this way. Instead you can do this:

string[] validValues = new string[] { "1", "5", "9", "whatever" };
if(validValues.Contains(foo))
{
    // do something
}

Solution 3 - C#

One possible option is this:

switch (foo)
{
    case "1":
    case "5":
    case "9":
        // your code here

        break;
}

Another possible option is this:

var vals = new string[] { "1", "5", "9" };
if (vals.Contains(foo))
{
    // your code here
}

Solution 4 - C#

If all options are just one character you could do:

if ("159".IndexOf(foo) != -1)
{
  //do something
}

Solution 5 - C#

Here is yet another alternative:

bool x = new[] { "1", "5", "9" }.Any(a => a == "5"); //x == true
bool y = new[] { "1", "5", "9" }.Any(a => a == "8"); //y == false

It is better to use .Contains(foo) in this case, as the flexibility of the lambda is rather wasted here. If there was a complex expression that needed to be done, Any would be more useful.

Solution 6 - C#

You can do this, if that's acceptable to you:

if ( (new string[] {"1","9","5","6" }).Contains(foo))
{
    
}

Solution 7 - C#

You may use the switch statement:

switch (foo) {
    case "1":
    case "5":
    case "9":
        // ...
        break;
    case "2":
    case "4":
        // ...
        break;
}

If foo is a string, pay attention on case sensitivity.

Solution 8 - C#

If you have multiple if conditions you should always consider using switch statements as compiler will create Jumptables whereever possible to increase speed. You should take a look here for speed test. Thing to note here is that if number of conditions is big enough to cover overheads, C# compiler will also create a HashTable object.

So this is a better approach,

switch (foo) {
case "1":
case "5":
case "9":
    // ...
    break;
case "2":
case "4":
    // ...
    break;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKMCView Question on Stackoverflow
Solution 1 - C#Trevor PilleyView Answer on Stackoverflow
Solution 2 - C#ZbigniewView Answer on Stackoverflow
Solution 3 - C#Mike PerrenoudView Answer on Stackoverflow
Solution 4 - C#jsedanoView Answer on Stackoverflow
Solution 5 - C#GrayView Answer on Stackoverflow
Solution 6 - C#IcarusView Answer on Stackoverflow
Solution 7 - C#joeView Answer on Stackoverflow
Solution 8 - C#EhsanView Answer on Stackoverflow