Cloud Training (Online)




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);
}