[C#] Can you loop through all enum values?



Answers

foreach(Foos foo in Enum.GetValues(typeof(Foos)))
Question

This question already has an answer here:
How to enumerate an enum? 14 answers

public enum Foos
{
    A,
    B,
    C
}

Is there a way to loop through the possible values of Foos?

Basically?

foreach(Foo in Foos)



static void Main(string[] args)
{
    foreach (int value in Enum.GetValues(typeof(DaysOfWeek)))
    {
        Console.WriteLine(((DaysOfWeek)value).ToString());
    }

    foreach (string value in Enum.GetNames(typeof(DaysOfWeek)))
    {
        Console.WriteLine(value);
    }
    Console.ReadLine();
}

public enum DaysOfWeek
{
    monday,
    tuesday,
    wednesday
}






foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    ...
}



Links