How to enumerate an enum in C#

Waqas Anwar
26 August 2014
3264 Views

You can use following code snippet to enumerate an enum in C#

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

foreach (Suit suit in (Suit[])Enum.GetValues(typeof(Suit)))
{
   Console.WriteLine(suit);
}