[Flags]
enum Permission
{
None = 0,
Read = 1,
Edit = 2,
Delete = 4,
ChangePermission = 8,
FullControl = Read | Edit | Delete | ChangePermission
}
Permission permission = Permission.Read | Permission.Edit;
if ((permission & Permission.Delete) == Permission.Delete)
Console.WriteLine("Has permissions");
In .NET 4, a new method was introduced in order to relieve yourself from this ugly code – Enum.HasFlag. So instead of writing the above if statement, you can write it like so:
if (permission.HasFlag(Permission.Delete))
Console.WriteLine("Has permissions");
This method makes the code readable, simpler, and saves you time explaining to new developers what is a bitwise operation.
How the hell did I miss this method in the “what’s new in .NET 4” documentation … better late than never.