> ## 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.

# C# Flags Enum

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.

```csharp theme={"dark"}
[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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
// 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.

```csharp theme={"dark"}
// 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.

```csharp theme={"dark"}
// Toggles FlagB
activeFlags ^= ConfigurationFlags.FlagB;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
