Getting the max value of an enum

后端 未结 11 2244
旧时难觅i
旧时难觅i 2020-12-07 13:42

How do you get the max value of an enum?

11条回答
  •  时光说笑
    2020-12-07 14:02

    After tried another time, I got this extension method:

    public static class EnumExtension
    {
        public static int Max(this Enum enumType)
        {           
            return Enum.GetValues(enumType.GetType()).Cast().Max();             
        }
    }
    
    class Program
    {
        enum enum1 { one, two, second, third };
        enum enum2 { s1 = 10, s2 = 8, s3, s4 };
        enum enum3 { f1 = -1, f2 = 3, f3 = -3, f4 };
    
        static void Main(string[] args)
        {
            Console.WriteLine(enum1.one.Max());        
        }
    }
    

提交回复
热议问题