In .NET 4.0, Microsoft introduced the
Enum.HasFlag() method to the .NET libraries. While this function is extremely useful, it has some costs anyone invoking it should be aware of as it does not match up to its standard bit-check equivalent.
If you're familiar with bit flags, then you likely know the typical way of checking if a bit flag exists in a given value is usually done like so:
if ((value & flag) == flag)
Let's take a look at the disassembly of
public bool HasFlag(Enum flag)
throw new ArgumentException(Environment.GetResourceString(
"Argument_EnumTypeDoesNotMatch", new object
ulong num = Enum.ToUInt64(flag.GetValue());
ulong num2 = Enum.ToUInt64(this.GetValue());
return (num2 & num) == num;
The first thing to note is the type-safety check.
HasFlag() ensures that both your flag,
flag are of the same enumeration type. This is likely the most costly factor in regards to
The second thing to note is that when you pass a value to
HasFlag() it is boxed to an
Enum type. If you are not familiar with what boxing is, I suggest reading the following.
This is another of the few things that affect the performance of
The third thing to look at is the two calls to
GetValue() returns an object, so the value of your enumeration must first be boxed to an
object and then unboxed by
Enum.ToUInt64(). If you're wondering why they use
UInt64, it's because it's the largest primitive data-type an enumeration can be. This is to ensure
HasFlag() works for all enumeration types.
Lastly, we see the method ends with what we expected, the standard bit check. To sum up what we said above into a few short points about why
HasFlag() has performance costs, it's due to:
- Type verification/check
- Lots of boxing/unboxing
The first alternative (and the most obvious) would be to use the standard check. This method isn't as nice looking as
HasFlag() but it definitely is the most efficient.
public enum ExampleEnum
A = 1 << 0,
B = 1 << 1,
C = 1 << 2,
D = 1 << 3,
E = 1 << 4,
public class Program
static void Main(string args)
var foo = ExampleEnum.A | ExampleEnum.B | ExampleEnum.C;
if ((foo & ExampleEnum.B) == ExampleEnum.B)
The second alternative would be to create an extension method for your enumeration. However, this can quickly get tedious if you
have a lot of enumerations that you wish to check. This method is still more efficient than using
Enum.HasFlag() as it does not involve any boxing
and it does not require any type verification.
public static bool HasBitFlag(this ExampleEnum e, ExampleEnum other)
return ((e & other) == other);
A third more generic (no pun intended, I swear!) solution would be to create a method such as the following:
public static bool HasBitFlag<T>(T e, T other)
where T : IConvertible
var eFlag = Convert.ToUInt64(e);
var otherFlag = Convert.ToUInt64(other);
return ((eFlag & otherFlag) == otherFlag);
This is a bit better than
Enum.HasFlag() because it does not require type-safety verification. However, it should be noted both
other are boxed to objects when invoking
The fourth and final alternative requires
inline IL. This solution exploits the fact that enumerations
of integer-types are essentially equivalent to their underlying integer-type.
public static class BitFlag
public static bool Has<T>(T val, T flag)
where T : struct
public static T SetState<T>(T val, T flag, bool on)
where T : struct
Points of Interest
I wasn't aware of this until a few months after the original release of .NET 4.0 when I decided to go snooping to find out how they implemented
the function (as the bitwise
& operator does not work on two types of
Enum). In most cases you don't have to be very concerned about
the performance implications of using a function like
Enum.HasFlag() as it is rare to be writing this sort of application in a managed language like C#.
I imagine there will come a day when you can use the type constraint
where T : enum (and also
where T : delegate although not relevant to this tip/article) and solution #4 will be achievable in regular C# code.