How to Loop through or Iterate over all Enum Values in C# ?

Sometimes , it may be necessary for the C# developers to loop through or iterate over all the Enum Values in solving a particular task. In this blog post , i will explain with an example on How to Loop through or Iterate over all Enum Values in C# ?

We use the keyword enum to create enumeration in c#.

For example , and enum of movies can be created by

enum Movies 

In the above example , Thuppaki will be assigned 0 , Maatran will be assigned 1 , Ghilli will be assigned 2 and Nanban will be assigned 3.

The methods Enum.Getnames and Enum.GetValues are used in c# to retreive the enum names and its values .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
    static class Program
        static void Main()
        enum Movies { Thuppaki, Maatran, Ghilli, Nanban};
        static void Traverse()
            foreach(var i in Enum.GetNames(typeof(Movies)))
            foreach (var i in Enum.GetValues(typeof(Movies)))
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

One Response

  1. Safi April 3, 2014 Reply

Leave a Reply


Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...