Export (0) Print
Expand All

Enum.HasFlag Method

Determines whether one or more bit fields are set in the current instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

member HasFlag : 
        flag:Enum -> bool 

Parameters

flag
Type: System.Enum
An enumeration value.

Return Value

Type: System.Boolean
true if the bit field or bit fields that are set in flag are also set in the current instance; otherwise, false.

ExceptionCondition
ArgumentException

flag is a different type than the current instance.

The HasFlag method returns the result of the following Boolean expression.

thisInstance And flag = flag 

If the underlying value of flag is zero, the method returns true. If this behavior is not desirable, you can use the Equals method to test for equality with zero and call HasFlag only if the underlying value of flag is non-zero, as the following example illustrates.

No code example is currently available or this language may not be supported.

The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute. For enumeration types that are not marked with the FlagsAttribute attribute, call either the Equals method or the CompareTo method.

The following example defines an ItemsOrdered enumeration that reflects categories of items that a customer can order in a restaurant. The example tests whether the customer has ordered both an entrée and a beverage.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft