If you’ve ever used enumerations in .NET with bitwise operations, using the Flags attribute, you should be familiar with the following piece of code:
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)
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:
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.