C# Tips & Tricks #5 – Enumerate an enum in C#

You can easily enumerate an enum in C# using the Enum.GetValues static method which returns the array.

How to enumerate an enum in C# ?

public enum Movie
{
             Viswasam,
             Sarkar,
             Petta,
             Kaththi
}

For example , assume that you have an enum called Movies with the following values.

You can enumerate the enum as shown below

foreach (Movie movie in (Movie[])Enum.GetValues(typeof(Movie)))
{
                 Console.WriteLine(movie);
}
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.