Getting the max value of an enum

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

How do you get the max value of an enum?

11条回答
  •  醉酒成梦
    2020-12-07 14:08

    This is slightly nitpicky but the actual maximum value of any enum is Int32.MaxValue (assuming it's a enum derived from int). It's perfectly legal to cast any Int32 value to an any enum regardless of whether or not it actually declared a member with that value.

    Legal:

    enum SomeEnum
    {
        Fizz = 42
    }
    
    public static void SomeFunc()
    {
        SomeEnum e = (SomeEnum)5;
    }
    

提交回复
热议问题