Getting the max value of an enum

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

How do you get the max value of an enum?

11条回答
  •  被撕碎了的回忆
    2020-12-07 14:13

    In agreement with Matthew J Sullivan, for C#:

       Enum.GetValues(typeof(MyEnum)).GetUpperBound(0);
    

    I'm really not sure why anyone would want to use:

       Enum.GetValues(typeof(MyEnum)).Cast().Last();
    

    ...As word-for-word, semantically speaking, it doesn't seem to make as much sense? (always good to have different ways, but I don't see the benefit in the latter.)

提交回复
热议问题