Enum.GetValues() Return Type

后端 未结 6 641
孤独总比滥情好
孤独总比滥情好 2020-12-01 11:45

I have read documentation that states that ‘given the type of the enum, the GetValues() method of System.Enum will return an array of the given enum\'s base type’ ie int, by

6条回答
  •  醉话见心
    2020-12-01 12:23

    Personally I've created a separate method in my Utils project, which I include in my other projects. Here's the code I use:

    public static class EnumUtil
    {
        public static IEnumerable GetAllValues() 
            where TEnum : struct, IConvertible, IComparable, IFormattable
        {
            return Enum.GetValues(typeof(TEnum)).Cast();
        }   
    }
    

    And I call it like this:

    var enumValues = EnumUtil.GetAllValues();
    

提交回复
热议问题