Cloud Training (Online)




C# Tips and 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);
}
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.