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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...