7

The common way of checking equality on multiple variables within an if condition is as following.

public enum Values
{
    Value1,
    Value2,
    Value3
}
void MethodName (Values randomValue )
{
 if (randomValue == Values.Value1|| randomValue == Values.Value2)
  {
       // code here
  }
}

Rather than having an OR condition, is there a better way of doing this ?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Nilaksha Perera
  • 715
  • 2
  • 12
  • 36
  • better option will be a switch – sujith karivelil May 31 '16 at 03:18
  • "common way of checking equality on multiple variables" is actually different - http://stackoverflow.com/questions/3907299/if-statements-matching-multiple-values... But you already should have [found](https://www.bing.com/search?q=c%23+check+equal+multiple) it before and disliked. For future questions please clarify your preference (otherwise it look like you have not done any research and ...) – Alexei Levenkov May 31 '16 at 03:34
  • Enums better to work in switch statements rather than if blocks. If you want to have multiple equality conditions, define logic for a main case and use `goto case` which point to main case for another related cases. – Tetsuya Yamamoto May 31 '16 at 03:42

3 Answers3

11

A few options:

  1. You can define your enums as flags. This means each value must be a power of 2 (1, 2, 4, 8, etc). Then, you could write:

if (randomValue & (Values.Value1 | Values.Value2) > 0)
{
     //...
}
  1. You can use a switch

switch (randomValue)
{
    case Values.Value1:
    case Values.Value2:
    {
        //Do something
        break;
    }
    case Values.Value3:
        //Do something else
        break;
    default:
        break;
}
  1. You can use an array (nicer if you have predefined sets of values you want to search for).

if (new[] { Values.Value1, Values.Value2 }.Contains(randomValue))
{
}

or

static(?) readonly Values[] allowedValues = new[] { Values.Value1, Values.Value2 };

void MethodName(Values randomValue)
{
    if (allowedValues.Contains(randomValue))
    {
        //...
    }
}
Community
  • 1
  • 1
Rob
  • 26,989
  • 16
  • 82
  • 98
  • 1
    how about the other way around, sounds like the question could be "How to check if a value of a variable is defined as an enum value?" and then do `if (Enum.IsDefined(typeOf(YourEnum, YourValue) { }`? works for me :) – Jane Jun 11 '19 at 15:23
  • 1
    I do like the bitwise discussion in the flags post, thanks for posting! – Jane Jun 11 '19 at 15:25
  • @JanneAndersson That won't work for their given example, though, which is checking if it's `Value1`, `Value2`, but *not* `Value3` – Rob Jun 14 '19 at 04:33
0

By using Enum Flags we can do comparisons as follows:

[Flags]
public enum Values
{
  Value1 = 1 << 0,
  Value2 = 1 << 1,
  Value3 = 1 << 2
}

void MethodName(Values randomValue)
{
  if ((randomValue & (Values.Value1 | Values.Value2)) != 0)
  {
    // code here
  }
}

Note the Flags attribute and the bit shifting required for the Enum values. The Enum values can also be defined as:

  [Flags]
  public enum Values
  {
    Value1 = 1,
    Value2 = 2,
    Value3 = 4
  }

We can now parameterize the Allowed Values as follows:

      [Flags]
      public enum Values
      {
        Value1 = 1 << 0,
        Value2 = 1 << 1,
        Value3 = 1 << 2
      }    

      void MethodName(Values randomValue)
      {
        if (IsValid(randomValue, Values.Value1 | Values.Value2))
        {
          // code here
        }
      }

      bool IsValid(Values randomValue, Values allowedValues)
      {
        return ((randomValue & (allowedValues)) != 0);
      }
-2

See this example

public enum Level {

HIGH,
MEDIUM,
LOW

}

You can refer to the constants in the enum above like this:

Level level = Level.HIGH;

Level level = ... //assign some Level constant to it

if( level == Level.HIGH) {

} else if( level == Level.MEDIUM) {

} else if( level == Level.LOW) {

}