如何获取枚举的最大值?
最佳答案
Enum.GetValues() 似乎按顺序返回值,因此您可以这样做:
// given this enum:
public enum Foo
{
Fizz = 3,
Bar = 1,
Bang = 2
}
// this gets Fizz
var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Last();
编辑
对于那些不愿意阅读评论的人:你也可以这样做:
var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Max();
...当您的某些枚举值为负时,这将起作用。
关于c# - 获取枚举的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/203377/