Enum.GetNames() results in unexpected order with negative enum constants

Viewed 3144

I have the following enum definition (in C#):

public enum ELogLevel
{
    General = -1,  // Should only be used in drop-down box in Merlinia Administrator log settings
    All = 0,       // Should not be used as a level, only as a threshold, effectively same as Trace
    Trace = 1,
    Debug = 2,
    Info = 3,
    Warn = 4,
    Error = 5,
    Fatal = 6,
    Off = 7        // Should not be used as a level, only as a threshold
}

Now, when I do an Enum.GetNames() on this type I get a string array with 9 elements as expected, but the order is All, Trace, ... , Off, General, which is not what I was expecting.

Here's the MSDN documentation for Enum.GetNames():

"Remarks: The elements of the return value array are sorted by the values of the enumerated constants."

What's going on here? I can change my program to take this "functionality" into account, but I'd kind of like to know why .NET is doing what it's doing.

2 Answers
Related