开发者

Best way to compare .NET enum values

What's the best way in code to 开发者_StackOverflowcompare enum values? For example, if I have the following enum type:

public enum Level : short {
    Low,   
    FairlyLow,
    QuiteLow,
    NotReallyLow,
    GettingHigh,
    PrettyHigh,
    High,
    VeryHigh,
}

And I want to be able to write statements such as:

from v in values select v where v > Level.QuiteLow


You need to cast the enum value to its numeric value, because enum values aren't comparable :

from v in values where (short)v > (short)Level.QuiteLow select v

EDIT: actually this is not true : enum values are comparable, so this code works fine :

from v in values where v > Level.QuiteLow select v
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜