System.Enum.HasFlag Method

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

Syntax

public bool HasFlag (Enum flag)

Parameters

flag
An enumeration value.

Returns

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

Remarks

The Enum.HasFlag(Enum) method returns the result of the following Boolean expression.

Example

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 Enum.Equals(object) method to test for equality with zero and call Enum.HasFlag(Enum) only if the underlying value of flag is non-zero, as the following example illustrates.

code reference: System.Enum.HasFlag#1

The Enum.HasFlag(Enum) method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute and can be used to determine whether multiple bit fields are set. For enumeration types that are not marked with the FlagsAttribute attribute, call either the Enum.Equals(object) method or the Enum.CompareTo(object) method.

Requirements

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