0

I am comparing a variable to many enum values, but the IntelliSense is giving me this warning:

*Bitwise operation on enum which is not marked by [Flags] attribute.*

if (val == MyEnum.Value1 | MyEnum.Value2 | MyEnum.Value3){
   //code
}

My enum looks like this:

public enum MyEnum
{
    [Description("Value1")]
    Value1 = 0,
    [Description("Value2")]
    Value2 = 1,
    [Description("Value3")]
    Value3 = 2,
}

What is it and what should I do? Is it safe? All i want to avoid is having to write the long version of an if multi-value comparison block.

garry man
  • 445
  • 4
  • 14
  • 2
    You mentioned that you are trying to avoid the long version of a multi-value comparison block. With the way your enum is defined, your short version just checks `if (val == 0 | 1 | 2)`, which can be simplified to `if (val == 3)`. If you want to check `if (val == 0 || val == 1|| val == 2)`, you need to use the long version. – Tobbs Dec 14 '18 at 19:03
  • @BenJ that's the answer I was looking for. – garry man Dec 14 '18 at 19:03

0 Answers0