enums - c# while loop - How to enumerate an enum

types of loops in c# / c# / .net / loops / enumeration

How can you enumerate an enum in C#?

E.g. the following code does not compile:

public enum Suit
{
    Spades,
    Hearts,
    Clubs,
    Diamonds
}

public void EnumerateAllSuitsDemoMethod()
{
    foreach (Suit suit in Suit)
    {
        DoSomething(suit);
    }
}

Peter Mortensen



Answer #1

I think you can use

Enum.GetNames(Suit)