How to enumerate an enum

后端 未结 29 1896
深忆病人
深忆病人 2020-11-22 01:14

How can you enumerate an enum in C#?

E.g. the following code does not compile:

public enum Suit
{         


        
29条回答
  •  梦谈多话
    2020-11-22 01:35

    If you have:

    enum Suit
    {
       Spades,
       Hearts,
       Clubs,
       Diamonds
    }
    

    This:

    foreach (var e in Enum.GetValues(typeof(Suit)))
    {
        Console.WriteLine(e.ToString() + " = " + (int)e);
    }
    

    Will output:

    Spades = 0
    Hearts = 1
    Clubs = 2
    Diamonds = 3
    

提交回复
热议问题