How to enumerate an enum

后端 未结 29 1835
深忆病人
深忆病人 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:36

    I tried many ways and got the result from this code:

    For getting a list of int from an enum, use the following. It works!

    List listEnumValues = new List();
    YourEnumType[] myEnumMembers = (YourEnumType[])Enum.GetValues(typeof(YourEnumType));
    foreach ( YourEnumType enumMember in myEnumMembers)
    {
        listEnumValues.Add(enumMember.GetHashCode());
    }
    

提交回复
热议问题