System.FlagsAttribute Class

Indicates that an enumeration can be treated as a bit field; that is, a set of flags.

See Also: FlagsAttribute Members


[System.AttributeUsage(System.AttributeTargets.Enum, Inherited=false)]
public class FlagsAttribute : Attribute


Bit fields are generally used for lists of elements that might occur in combination, whereas enumeration constants are generally used for lists of mutually exclusive elements. Therefore, bit fields are designed to be combined with a bitwise OR operation to generate unnamed values, whereas enumerated constants are not. Languages vary in their use of bit fields compared to enumeration constants.

Attributes of the FlagsAttribute

AttributeUsageAttribute is applied to this class, and its AttributeUsageAttribute.Inherited property specifies false. This attribute can only be applied to enumerations.

Guidelines for FlagsAttribute and Enum

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.


The following example demonstrates the use of FlagsAttribute on the formatting of a Enum. With this attribute, the Position enumeration is used as a bit-field, and the value 3 (Top | Left) is considered a valid value for the enumeration when formatting. Without this attribute, the enumeration Color is not used as a bit-field, and the value 3 (Red | Blue) is not considered a valid value for the enumeration when formatting.

C# Example

using System;
public enum Position { 

  Top = 0x1, 
  Left = 0x2, 
  Bottom = 0x4, 
  Right = 0x8 

//enum Color declared without FlagsAttribute 
public enum Color { 

  Red = 0x1, 
  Blue = 0x2, 
  Yellow = 0x4 

public class enumFormat { 

  public static void Main() { 

    Position p = Position.Top | Position.Left; 
    Console.WriteLine("Position: {0}", p); 
    Color c = Color.Red | Color.Blue; 
    Console.WriteLine("Color: {0}", c); 

The output is

Position: Top, Left
Color: 3


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,