Better Know A Framework: System.Enum.HasFlag()

Ok, so first ‘Better Know A Framework’. This one is for the new .NET 4.0 method HasFlag on System.Enum

I chose this one because a while back in my blog I posted an Enum helper that had a method like this. The method just replaces the:

bool isFlagSet = (value & flag == flag);

with:

bool isFlagSet = value.HasFlag(flag);

Another helpful little thing is that the flag value you pass in can also be a combination of flag values. Its simple but helpful. However on my helper, I also put the SetFlag method. Maybe Microsoft are working that one up for .NET 5

So that’s it for the first of these, there is the new method, give it a whirl

Advertisements
This entry was posted in Better Know a Framework. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s