Qwerty
Qwerty

Reputation: 307

Best way to compare .NET enum values

What's the best way in code to compare 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

Upvotes: 1

Views: 1778

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292405

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

Upvotes: 4

Related Questions