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
精彩评论