How to enumerate an enum in c#
We can use the enum.GetNames()
method to enumerate through a enum in c#.
public enum Fruits
{
Orange,
Grapes,
Apple
}
public void PrintAllFruits()
{
foreach (string fruit in Enum.GetNames(typeof(Fruits)))
{
System.Console.WriteLine(fruit);
}
}
In the above example, we are iterating through a Fruits
enum and printing each enum in the console.
Iterating through enum values
If you want enum values instead of names you need to use enum.GetValues()
method.
public void PrintAllFruits()
{
foreach (string fruit in Enum.GetValues(typeof(Fruits)))
{
System.Console.WriteLine(fruit);
}
}