SAM SAM - 22 days ago 7
C# Question

How to combine many OR statements in an if statement

Say this:

if (stars == 2 || stars ==6 || stars ==10)
{
do something
}


is there's a way to combined them together as if:

if (stars == {2, 4, 6}) <--- MATLAB style
{
do something
}

Answer

you could write an extension like this:

public static class GenericExtensions
{
    public static bool In<T>(this T @this, params T[] listOfItems)
    {
        if (null == listOfItems) return false;
        return listOfItems.Contains(@this);
    }
}

and then use it like:

if (2.In(1,2,3,4))