Bitwise Manipulation using enums C# with Example



Bitwise Manipulation using enums C# with Example

The FlagsAttribute should be used whenever the enumerable represents a collection of flags, rather than a single 
value. The numeric value assigned to each enum value helps when manipulating enums using bitwise operators. 
Example 1 : With [Flags] 
[Flags] 
enum Colors 
{ 
Red=1, 
Blue=2, 
Green=4, 
Yellow=8 
} 
var color = Colors.Red | Colors.Blue; 
Console.WriteLine(color.ToString()); 
prints Red,Blue 
Example 2 : Without [Flags] 
enum Colors 
{ 
Red=1, 
Blue=2, 
Green=4, 
Yellow=8 
} 
var color = Colors.Red | Colors.Blue; 
Console.WriteLine(color.ToString()); 
prints 3 
 

0 Comment's

Comment Form

Submit Comment