> For the complete documentation index, see [llms.txt](https://docs.lfinteractive.net/programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lfinteractive.net/programming/csharp/undestanding-csharp/enum.md).

# Enum

Enums, short for "enumerations," are a fundamental data type in C#. They allow you to define a named set of constant values representing distinct integer values. Enums are often used to make code more readable and maintainable by providing meaningful names for numeric constants.

### Declaring an Enum

You can declare an enum using the `enum` keyword in C#. Here's the basic syntax:

```csharp
enum DaysOfWeek
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}
```

In this example, we've defined an enum called `DaysOfWeek` with seven named constants. By default, the underlying values are assigned starting from 0 and incrementing by 1 for each subsequent element. So, `Sunday` is assigned a value of 0, `Monday` is 1, and so on.

### Explicit Enum Values

You can assign specific integer values to enum members if needed:

```csharp
enum DaysOfWeek
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7
}
```

In this example, we've assigned custom values to each day of the week.

### Using Enums

Once you've defined an enum, you can use it in your code. For example, you can declare variables of the enum type and assign enum values to them:

```csharp
DaysOfWeek today = DaysOfWeek.Wednesday;
```

You can also use enums in switch statements to make your code more readable:

```csharp
switch (today)
{
    case DaysOfWeek.Monday:
        Console.WriteLine("It's Monday.");
        break;
    case DaysOfWeek.Wednesday:
        Console.WriteLine("It's Wednesday.");
        break;
    // ...
    default:
        Console.WriteLine("It's not a weekday.");
        break;
}
```

### Converting Enums

You can convert between enum values and their underlying integer values using casting:

```csharp
int dayValue = (int)DaysOfWeek.Monday; // Converts Monday enum to 2
DaysOfWeek anotherDay = (DaysOfWeek)3; // Converts integer 3 to Tuesday
```

Enums are a useful feature in C# for improving code clarity, reducing the risk of errors, and making your code more maintainable by using meaningful names for constant values.
