Check if a variable is in an ad-hoc list of values

C#VariablesContains

C# Problem Overview


Is there a shorter way of writing something like this:

if(x==1 || x==2 || x==3) // do something

What I'm looking for is something like this:

if(x.in((1,2,3)) // do something

C# Solutions


Solution 1 - C#

You could achieve this by using the List.Contains method:

if(new []{1, 2, 3}.Contains(x))
{
    //x is either 1 or 2 or 3
}

Solution 2 - C#

public static bool In<T>(this T x, params T[] set)
{
    return set.Contains(x);
}

...

if (x.In(1, 2, 3)) 
{ ... }

Required reading: MSDN Extension methods

Solution 3 - C#

If it's in an IEnumerable<T>, use this:

if (enumerable.Any(n => n == value)) //whatever

Else, here's a short extension method:

public static bool In<T>(this T value, params T[] input)
{
    return input.Any(n => object.Equals(n, value));
} 

Put it in a static class, and you can use it like this:

if (x.In(1,2,3)) //whatever

Solution 4 - C#

int x = 1;
if((new List<int> {1, 2, 3}).Contains(x))
{
}

Solution 5 - C#

I'm entirely guessing here, correct the code if I'm wrong:

(new int[]{1,2,3}).IndexOf(x)>-1

Solution 6 - C#

You can create a simple Dictionary<TKey, TValue> that'll be used as a Decision Table for that problem:

        //Create your decision-table Dictionary
        Action actionToPerform1 = () => Console.WriteLine("The number is okay");
        Action actionToPerform2 = () => Console.WriteLine("The number is not okay");
        var decisionTable = new Dictionary<int, Action>
            {
                {1, actionToPerform1},
                {2, actionToPerform1},
                {3, actionToPerform1},
                {4, actionToPerform2},
                {5, actionToPerform2},
                {6, actionToPerform2}
            };

        //According to the given number, the right *Action* will be called.
        int theNumberToTest = 3;
        decisionTable[theNumberToTest](); //actionToPerform1 will be called in that case.

Once you've initialized your Dictionary, all left to do is:

decisionTable[theNumberToTest]();

Solution 7 - C#

This answer refers to a possible future version of C# ;-) If you consider switching to Visual Basic, or if Microsoft finally decides to introduce the Select Case statement to C#, it would look like this:

Select Case X
    Case 1, 2, 3
    ...
End Select

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
Questionuser1854438View Question on Stackoverflow
Solution 1 - C#Ilya IvanovView Answer on Stackoverflow
Solution 2 - C#Austin SalonenView Answer on Stackoverflow
Solution 3 - C#It'sNotALie.View Answer on Stackoverflow
Solution 4 - C#RacerNerdView Answer on Stackoverflow
Solution 5 - C#IsaacView Answer on Stackoverflow
Solution 6 - C#Yair NevetView Answer on Stackoverflow
Solution 7 - C#TigerfinkView Answer on Stackoverflow