Add and remove values from flagged enum C# with Example



Add and remove values from flagged enum C# with Example

This code is to add and remove a value from a flagged enum-instance: 
[Flags] 
public enum MyEnum 
{ 
Flag1 = 1 << 0, 
Flag2 = 1 << 1, 
Flag3 = 1 << 2 
} 
var value = MyEnum.Flag1; 
// set additional value 
value |= MyEnum.Flag2; //value is now Flag1, Flag2 
value |= MyEnum.Flag3; //value is now Flag1, Flag2, Flag3 
// remove flag 
value &= ~MyEnum.Flag2; //value is now Flag1, Flag3 

0 Comment's

Comment Form

Submit Comment