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

An enumeration (`enum`) is a distinct value type defined by a set of named integral constants. At runtime, an `enum` is resolved to its underlying numeric type, meaning it carries the same memory footprint and performance characteristics as a standard primitive integer. All enums implicitly inherit from the abstract class `System.Enum`, which in turn inherits from `System.ValueType`.

## Syntax and Initialization

By default, the underlying type of an enum is `int`. The first enumerator has the value `0`, and the value of each successive enumerator is incremented by `1`.

```csharp theme={"dark"}
enum ConnectionState
{
    Disconnected, // 0
    Connecting,   // 1
    Connected     // 2
}
```

You can override the default indexing by explicitly assigning values. Unassigned enumerators will automatically increment from the last explicitly assigned value.

```csharp theme={"dark"}
enum StatusCode
{
    Success = 0,
    Redirect = 300,
    NotFound = 404,
    InternalError     // Automatically assigned 405
}
```

## Underlying Types

An enum can be declared with any integral numeric type except `char`. Supported types are `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long`, and `ulong`. The C# specification requires enum underlying types to have a deterministic size for memory layout and interop, which is why architecture-dependent types like `nint` and `nuint` are not permitted. Specifying a smaller underlying type reduces the memory footprint of the struct or class containing the enum.

```csharp theme={"dark"}
enum ProcessLevel : byte
{
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 255 // Maximum value for a byte
}
```

## Type Conversion

Because enums are strongly typed, implicit conversions between an `enum` and its underlying integral type are generally not allowed, requiring explicit casting. The sole exception is the decimal integer literal `0`, which the C# specification permits to be implicitly converted to any enum type.

```csharp theme={"dark"}
// Implicit conversion of the decimal literal 0 is allowed
StatusCode defaultCode = 0; // Valid without a cast

// Enum to Integer
int numericValue = (int)StatusCode.NotFound; // 404

// Integer to Enum (requires explicit cast for non-zero values)
StatusCode code = (StatusCode)300; // StatusCode.Redirect

// Note: Casting an undefined integer does not throw an exception
StatusCode undefinedCode = (StatusCode)999; 
```

## Bitwise Enumerations (`[Flags]`)

The `[Flags]` attribute indicates that an enumeration can be treated as a bit field. This allows a single enum variable to store multiple values simultaneously using bitwise operations. When defining a flags enum, values must be powers of two (using bit shift operators is standard practice).

```csharp theme={"dark"}
[Flags]
enum FilePermissions : short
{
    None    = 0,
    Read    = 1 << 0, // 1
    Write   = 1 << 1, // 2
    Execute = 1 << 2, // 4
    All     = Read | Write | Execute // 7
}
```

Bitwise operators (`|`, `&`, `~`, `^`) and the `HasFlag` method are used to manipulate and evaluate flag combinations.

```csharp theme={"dark"}
FilePermissions access = FilePermissions.Read | FilePermissions.Write; // 3

// Check if a flag is set using HasFlag
bool canWrite = access.HasFlag(FilePermissions.Write); // True

// Check if a flag is set using bitwise AND (more performant in older .NET versions)
bool canExecute = (access & FilePermissions.Execute) == FilePermissions.Execute; // False

// Toggle a flag using bitwise XOR
access ^= FilePermissions.Write; // Removes Write flag
```

## Core `System.Enum` Methods

The `System.Enum` base class provides static methods for reflection, parsing, and manipulation of enum types.

**Parsing Strings to Enums:**

```csharp theme={"dark"}
// Throws ArgumentException if parsing fails
ConnectionState state1 = (ConnectionState)Enum.Parse(typeof(ConnectionState), "Connected");

// Safe parsing (returns boolean)
if (Enum.TryParse("Connecting", true, out ConnectionState state2)) // true ignores case
{
    // state2 is ConnectionState.Connecting
}
```

**Retrieving Metadata:**

```csharp theme={"dark"}
using System.Linq;

// Get all numeric values in the enum. 
// Enum.GetValues returns an array of the enum type (StatusCode[]). 
// Because unboxing in C# requires the exact type, elements must be cast to the enum type 
// first before projecting to the underlying integer.
int[] values = Enum.GetValues(typeof(StatusCode))
                   .Cast<StatusCode>()
                   .Select(e => (int)e)
                   .ToArray();

// Alternatively, cast directly to the enum array type:
StatusCode[] enumValues = (StatusCode[])Enum.GetValues(typeof(StatusCode));

// In .NET 5 and later, a generic overload simplifies this:
// StatusCode[] modernEnumValues = Enum.GetValues<StatusCode>();

// Get all string names in the enum
string[] names = Enum.GetNames(typeof(StatusCode));

// Check if a specific integer exists in the enum definition
bool isDefined = Enum.IsDefined(typeof(StatusCode), 404); // True
```

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