Enum values in a list

前端 未结 5 1038
悲&欢浪女
悲&欢浪女 2021-02-20 11:38

I\'ve a enum class like,

public enum USERTYPE
{
   Permanant=1,
   Temporary=2,
}

in my business object I just declare this enum as

<         


        
5条回答
  •  -上瘾入骨i
    2021-02-20 12:16

    You can use this to get all individual enum values:

    private List userTypes = Enum.GetValues(typeof(USERTYPE)).Cast().ToList();
    

    If you do things like this more often, you could create a generic utility method for it:

    public static T[] GetEnumValues() where T : struct {
        if (!typeof(T).IsEnum) {
            throw new ArgumentException("GetValues can only be called for types derived from System.Enum", "T");
        }
        return (T[])Enum.GetValues(typeof(T));
    }
    

提交回复
热议问题