Enum in C# 11

Enums are powerful data types in C# that allow you to define a set of named constants. They are often used to represent a fixed number of possible values that a variable can take on.

Here's a step-by-step tutorial on how to use enums in C#:

Step 1. Define an enum

To define an enum, use the enum keyword followed by the name of the enum. Here's an example:

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

In this example, we've defined an enum called DaysOfWeek with seven possible values: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. Note that the values of an enum are constants and are not allowed to change during program execution.

Step 2. Declare a variable of the enum type

To declare a variable of the enum type, simply use the enum name followed by the variable name. Here's an example:

DaysOfWeek today = DaysOfWeek.Monday;

In this example, we've declared a variable called today of type DaysOfWeek and assigned it the value of Monday.

Step 3. Use the enum variable

You can use the enum variable just like any other variable. Here are some examples:

if (today == DaysOfWeek.Saturday || today == DaysOfWeek.Sunday)
{
    Console.WriteLine("It's the weekend!");
}
else
{
    Console.WriteLine("It's a weekday.");
}

switch (today)
{
    case DaysOfWeek.Monday:
        Console.WriteLine("Back to work!");
        break;
    case DaysOfWeek.Friday:
        Console.WriteLine("TGIF!");
        break;
    default:
        Console.WriteLine("Just another day...");
        break;
}

In the first example, we used an if statement to check whether today is a weekend day or a weekday. In the second example, we've used a switch statement to print a different message depending on which day of the week today is.

Step 4. Enum underlying type

By default, the underlying type of an enum is int. However, you can specify a different underlying type if needed. Here's an example:

enum StatusCode : byte
{
    OK = 200,
    NotFound = 404,
    InternalServerError = 500
}

In this example, we've defined an enum called StatusCode with three possible values, and we've specified that the underlying type is byte instead of int.

Step 5. Enum attributes

You can also add attributes to enum values if needed. Here's an example:

enum Colors
{
    [Description("Red color")]
    Red,
    [Description("Blue color")]
    Blue,
    [Description("Green color")]
    Green
}

In this example, we've added a Description attribute to each enum value to provide a human-readable description of each color.

That's it! You now know how to define and use enums in C#.

Top 5 FAQs on C# enums

Here are five frequently asked questions about C# enums and their answers:

Can I assign a value to an enum member?

Yes, you can assign a specific value to each enum member. By default, the first member is assigned the value 0, and each subsequent member is assigned a value that is one greater than the previous member. However, you can override these default values by specifying the value explicitly. Here's an example:

enum Status
{
    Pending = 1,
    Approved = 2,
    Rejected = 3
}

In this example, we've assigned specific values to each enum member.

Can I use an enum as a method parameter or return type?

Yes, you can use an enum as a parameter or return type in a method. Here's an example:

enum Status
{
    Pending,
    Approved,
    Rejected
}

public Status GetStatus()
{
    // implementation here
}

public void UpdateStatus(Status newStatus)
{
    // implementation here
}

In this example, we've defined a method called GetStatus that returns a value of type Status, and a method called UpdateStatus that takes a parameter of type Status.

Can I convert an enum to a string?

Yes, you can convert an enum to a string using the ToString method. Here's an example:

enum Status
{
    Pending,
    Approved,
    Rejected
}

Status myStatus = Status.Approved;
string statusString = myStatus.ToString(); // "Approved"

In this example, we've converted the myStatus enum value to a string using the ToString method.

Can I convert a string to an enum?

Yes, you can convert a string to an enum using the Enum.Parse method. Here's an example:

enum Status
{
    Pending,
    Approved,
    Rejected
}

string statusString = "Approved";
Status myStatus = (Status)Enum.Parse(typeof(Status), statusString);

In this example, we've converted the statusString string to an enum value using the Enum.Parse method.

Can I iterate over the values of an enum?

Yes, you can iterate over the values of an enum using the Enum.GetValues method. Here's an example:

enum Status
{
    Pending,
    Approved,
    Rejected
}

foreach (Status status in Enum.GetValues(typeof(Status)))
{
    Console.WriteLine(status);
}

In this example, we've used a foreach loop to iterate over the values of the Status enum and print each one to the console.

Learn more about the Top 7 C# Enum Code Examples.


Recommended Free Ebook
Similar Articles