How to Loop through Enum Values in C#

Looping through an enumeration list or enum in C# is an essential skill. This tech-recipe provides a detailed example and walks through an explanation of the looping process.

The enumeration data type (or enum) is used to assign symbolic constants with unique values. The enum keyword is use in C# to declare an enumeration.

First, an enum will need to be declared using the following syntax:

enum identifier { enumerator-list }

Using this syntax we can build our example.

enum WeekDays { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

In above example the first symbolic constant ‘Sun’ will be assigned a value 0. ‘Mon’ will equal 1. This pattern would continue until ‘Sat’ equals the value of 6. Now, without looping, printing all the enum list items and their respective values could require up to seven lines of code. One for each day of the week.

Vishwanath Dalvi is a gifted engineer and tech enthusiast. He enjoys music, magic, movies, and gaming. When not hacking around or supporting the open source community, he is trying to overcome his phobia of dogs.View more articles by Vishwanath Dalvi