获取枚举的最大值 [英] Getting the max value of an enum

查看:166
本文介绍了获取枚举的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何枚举的最大值?

推荐答案

Enum.GetValues​​()似乎返回值顺序,所以你可以做这样的事情:

Enum.GetValues() seems to return the values in order, so you can do something like this:

// given this enum:
public enum Foo
{
    Fizz = 3, 
    Bar = 1,
    Bang = 2
}

// this gets Fizz
var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Last();

修改

对于那些不愿意通过评论阅读:你也可以这样来做:

For those not willing to read through the comments: You can also do it this way:

var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Max();



...这将工作时,你的一些枚举值是负的。

... which will work when some of your enum values are negative.

这篇关于获取枚举的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆