Enumerating an Enum in C#

Enumerating an Enum in C#

Enumerations, commonly known as enums, are a powerful feature that allows developers to define a set of named constants.

Introduction

Enumerations help improve code readability, maintainability, and type safety. While working with enums, you may often encounter scenarios where you need to enumerate or iterate over the values of an enum. This article demonstrates how to enumerate an enum in C# code.

Step 1: Define an Enum

To begin, let’s define a sample enum to work with. Consider the following example:

public enum Colors
{
    Red,
    Green,
    Blue
}

Step 2: Retrieve Enum Values

It provides a method called Enum.GetValues allows us to retrieve all the values of an enum. The method returns an array containing the values of the enum. Please find below the example of how to use it:

Colors[] colorValues = (Colors[])Enum.GetValues(typeof(Colors));

foreach (Colors color in colorValues)
{
    Console.WriteLine(color);
}

In the code snippet above, we retrieve the values of the Colors enum using Enum.GetValues. The method requires the enum type, which is obtained using the typeof operator. One way is to cast the result to an array of Colors and iterate over the values using a for-each loop. Finally, print each colour to the console.

Step 3: Enumerate with Casting

Another way to enumerate an enum is by casting and iterating over its underlying integral values. Each value in the enum has a corresponding integer value assigned to it. Obtain the integral value using the Convert.ToInt32 method or by casting it directly. Please find below the code snippet for the same.

int enumLength = Enum.GetValues(typeof(Colors)).Length;

for (int i = 0; i < enumLength; i++)
{
    Colors color = (Colors)i;
    Console.WriteLine(color);
}

The following code snippet determines the length of the enum by using Enum.GetValues(typeof(Colors)).Length. Then, in a for loop, we cast each integer value back to the Colors enum type and print the result.

Step 4: Enumerate with Enum.GetNames

It also provides the Enum.GetNames method returns an array of strings containing the names of the enum’s values. Please find below the code snippet for the same.

string[] colorNames = Enum.GetNames(typeof(Colors));

foreach (string colorName in colorNames)
{
    Console.WriteLine(colorName);
}

In this example, retrieve the names of the Colors enum values using Enum.GetNames and iterate over the resulting array using a for-each loop. We then print each colour name to the console.

Conclusion

Exploring an enum using the Enum.GetValues method, casting with integral values, or Enum.GetNames. This makes it effortless to traverse the significance or names of an enum and provides versatility to perform various operations on enum values within the code. It’s crucial to consider your specific requirements when choosing the best approach to access the values or their associated names.

Execute a stored procedure Dapper
Learn how to use the Dapper framework to execute a stored procedure in a C#
*application.*medium.com

Dapper Best Practices: C# Developers’ Guide to Database Management
*Dapper is an open-source ORM tool that is easy to use and lightweight, which makes it a popular choice for .NET…*
medium.com

Using Dapper with Entity Framework
*Various tools and libraries are available to developers working with data in a C# application. Two of the most popular…*
medium.com

ORM Wars: Dapper vs EF Core
The Battle
Continuesmedium.com

Maximizing C# Database Performance with Dapper
*Dapper is a popular open-source micro-ORM (Object Relational Mapping) framework developed by Stack Overflow. It…*
medium.com

Follow me on

C# Publication, LinkedIn, Instagram, Twitter, Dev.to

Did you find this article valuable?

Support C# Programming by becoming a sponsor. Any amount is appreciated!