Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Flags enumeration in C# is a specialized type of enum decorated with the [System.Flags] attribute, designed to treat the enumeration as a bit field. This allows a single enumeration variable to store a combination of multiple values simultaneously using bitwise operations, rather than being restricted to a single, mutually exclusive constant. To function correctly as a bit field, the underlying integer values of the enumeration members must be defined as powers of two (1, 2, 4, 8, 16, etc.). This ensures that each value occupies exactly one unique bit in the binary representation of the underlying integer type.

Declaration Syntax

When defining a Flags enum, developers typically use either explicit integer assignment or the bitwise left-shift operator (<<) to guarantee non-overlapping bit positions. It is standard practice to include a None member assigned to 0 to represent an empty bit field.
[Flags]
public enum ConfigurationFlags
{
    None   = 0,         // 00000000
    FlagA  = 1 << 0,    // 00000001 (1)
    FlagB  = 1 << 1,    // 00000010 (2)
    FlagC  = 1 << 2,    // 00000100 (4)
    FlagD  = 1 << 3,    // 00001000 (8)
    
    // Composite flags can be predefined using Bitwise OR
    FlagsAB = FlagA | FlagB // 00000011 (3)
}

The [Flags] Attribute Mechanics

The [Flags] attribute does not alter the underlying bitwise arithmetic; the bitwise operators will function on any integer-backed enum. Instead, the attribute modifies the metadata of the type and alters the behavior of System.Enum.ToString():
  • Enum.ToString(): When called on a standard enum containing a combined bitwise value that lacks a defined named constant, it returns the raw integer string. When called on an enum decorated with [Flags], the runtime parses the bit field and returns a comma-separated string of the active member names.
(Note: Enum.Parse() and Enum.TryParse() natively support parsing comma-separated strings into combined bitwise values for all enums. This parsing capability is built into the System.Enum class and is not dependent on the presence of the [Flags] attribute.)

Bitwise Operations

Manipulating and evaluating a Flags enum requires standard bitwise operators.

1. Combining Values (Bitwise OR |)

The | operator merges the bit patterns of two or more flags. If a bit is 1 in either operand, it becomes 1 in the result.
ConfigurationFlags activeFlags = ConfigurationFlags.FlagA | ConfigurationFlags.FlagC;
// Binary: 00000001 | 00000100 = 00000101

2. Checking Values (Bitwise AND & or HasFlag)

To determine if a specific bit is set, use the & operator to mask the enum, then compare the result to the target flag. Alternatively, the .NET Base Class Library provides the Enum.HasFlag() method. Starting in .NET Core 2.1, the .NET JIT compiler recognizes HasFlag as an intrinsic and optimizes it into the equivalent bitwise CPU instructions, avoiding the overhead of a standard method call. Checking for None: Checking for an empty bit field (None or 0) requires a direct equality check. Using the bitwise AND approach (activeFlags & ConfigurationFlags.None) == ConfigurationFlags.None or activeFlags.HasFlag(ConfigurationFlags.None) will always evaluate to true regardless of the active flags, because bitwise operations against zero always yield zero.
// Using Bitwise AND
bool hasFlagA = (activeFlags & ConfigurationFlags.FlagA) == ConfigurationFlags.FlagA;

// Using HasFlag method
bool hasFlagC = activeFlags.HasFlag(ConfigurationFlags.FlagC);

// Checking for None (0) - Must use direct equality
bool isNone = activeFlags == ConfigurationFlags.None;

3. Removing Values (Bitwise AND with Bitwise NOT & ~)

To unset a specific flag while leaving the rest of the bit field intact, combine the & operator with the bitwise complement operator (~). The ~ operator inverts the target flag’s bits, and the & operator applies that mask to clear only the target bit.
// Removes FlagA from the bit field
activeFlags &= ~ConfigurationFlags.FlagA;
// Binary: 00000101 & 11111110 = 00000100

4. Toggling Values (Bitwise XOR ^)

The ^ operator flips the state of a specific bit. If the flag is currently set, it will be removed. If it is not set, it will be added.
// Toggles FlagB
activeFlags ^= ConfigurationFlags.FlagB;
Master C# with Deep Grasping Methodology!Learn More