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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...