Getting the max value of an enum

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();

Edit

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)