开发者

Best way to compare .NET enum values

开发者 https://www.devze.com 2022-12-11 08:47 出处:网络
What\'s the best way in code to 开发者_StackOverflowcompare enum values?For example, if I have the following enum type:

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

精彩评论

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