Introduction to Enums in C#

C# Programming Tutorials
In the C# programming language, enum represents a user-defined value type that consists of integer constants. This programming tutorial discusses enums in C#, their benefits, and how they can be used through code examples.

Before we begin, you may want to read our tutorial: C# Data Types Explained if you need a refresher on data types.

What is an Enum in C#?

Enums provide a way to preserve a set of named constants. They are often used in C# programs to group related constants together, or they can be used to create sets of values.

Here is the syntax for declaring an enum in C#:

enum enum_name {
   enumeration list
};

The following is a code example of how to declare an enum in C#:

 
public enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

As you can see from the example above, each value in the enum is given a name. When you use enums in your code, you can refer to these names instead of their numeric values.

If a developer does not assign values to an enum explicitly, the first item in the list will have a value of 0. The next item in the list will have a value 1, and so on:

enum LogLevel
{
    Information,    //0
    Critical,       //1
    Error           //2
}

Since enums are strongly typed, programmers cannot convert enum values to another data type implicitly. However, you can convert them to your desired type by using explicit conversion.

Note that you can have two enum members with identical integer values:

enum Status
{
    NotStarted = 1,
    InProgress = 1,
    Complete = 2
}

Read: Implicit versus Explicit Conversions in C#

What are the Benefits of Enum in C#?

There are several reasons to use enums in C#. Enums can be used to represent a set of named constants, such as the days of the week. They can also be used to represent bit flags, which can make code more readable and maintainable.

Enums make your code concise, clearer, and easier to read in switch statements. Moreover, enums are value types and do not have reference semantics, and thus developers can pass them by value (or copy them) without worrying about whether the data might change from one place in your program to another.

How to Program Enums in C#

Enums are a great way to organize data in your C# code. They enable you to create a set of named constants that can be used throughout your code. Enums are especially useful when you need to represent a small set of related values, such as the days of the week or the seasons of the year.

To use an enum, simply declare it like you would any other variable type:

public enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

Then you can use the enum anywhere in your code:

Days today = Days.Monday;
if (today == Days.Saturday || today == Days.Sunday) {
Console.WriteLine("This is a weekend!");
}

How to Convert an Enum to an Integer in C#

Consider the following enum:

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

The following code example shows how you can display integer values of the items in the enum in C#:

Console.WriteLine("The integer value of Monday in the enum is " + (int)DaysOfWeek.Monday);
Console.WriteLine("The integer value of Tuesday in the enum is " + (int)DaysOfWeek.Tuesday);
Console.WriteLine("The integer value of Wednesday in the enum is " + (int)DaysOfWeek.Wednesday);

How to Convert an Enum to a String

You can convert an enum to a String in C# using the ToString() method. This method returns the name of the enum value as a String.

Here is some sample code showing how to convert an enum to a String in C# using the ToString() method:

enum MyEnum { Value1, Value2 };
string str = MyEnum.Value1.ToString();

The String object str now contains the value “Value1“.

Read: Top Task Management Software for Developers

Final Thoughts on Enums in C#

Here are some final thoughts on using enums in C#.

When to use Enums

Enums can be used when you need to represent a finite set of values, such as days of the week, months of the year, or different types of errors.

Creating an Enum

To create an enum, use the enum keyword followed by a name for the enum. The values in an enum are called members.

Accessing Enum Members

You can access enum members by using dot notation. For example, if you have an enum called Days with members Sunday and Monday, you would access them like this:

Days.Sunday or Days.Monday.

Specifying Enum Member Values

By default, the first member of an enum is assigned the value 0, and each subsequent member is assigned the next value in sequence. However, you can also explicitly specify member values if needed.

Converting Between Enums and Other Types

You can convert between enums and other types using typecasting. For example, a programmer could convert an enum member to an integer like this:

int day = (int)DaysOfWeek.Wednesday;

Conclusion

In C#, enums provide a way to group a collection of integer constants in a single unit. An enumeration type represents a choice from a collection of mutually exclusive values or a combination of values.

Read more C# programming tutorials and how-tos.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

More by Author

Previous article
Next article

Must Read