Bitwise Manipulation using enums
suggest changeApply FlagsAttribute when the enumeration represents a combination of flags, rather than a single value. The numeric value of each member is what the bitwise operators use.
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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents