enum MyEnum { Flag1 = 1, Flag2 = 2, Flag3 = 4 } MyEnum value = MyEnum.Flag1 | MyEnum.Flag3; if(value.HasFlag(MyEnum.Flag1)) { Console.WriteLine("Flag1 is set"); } else { Console.WriteLine("Flag1 is not set"); }
if(value.HasFlag(MyEnum.Flag1 | MyEnum.Flag2)) { Console.WriteLine("Flag1 and Flag2 are both set"); }In this example, we use the HasFlag method to check whether value has both Flag1 and Flag2 set at the same time. We do this by passing in a bitwise OR of the two flags as the input parameter to HasFlag. The System Enum HasFlag method is part of the System.Runtime.CompilerServices package library in C#.