32

I checked the section of the C# language specification regarding enums, but was unable to explain the output for the following code:

enum en {
    a = 1, b = 1, c = 1,
    d = 2, e = 2, f = 2,
    g = 3, h = 3, i = 3,
    j = 4, k = 4, l = 4
}

en[] list = new en[] {
    en.a, en.b, en.c,
    en.d, en.e, en.f,
    en.g, en.h, en.i,
    en.j, en.k, en.l
};
foreach (en ele in list) {
    Console.WriteLine("{1}: {0}", (int)ele, ele);
}

It outputs:

c: 1
c: 1
c: 1
d: 2
d: 2
d: 2
g: 3
g: 3
g: 3
k: 4
k: 4
k: 4

Now, why would it select the third "1", the first "2" and "3", but the second "4"? Is this undefined behavior, or am I missing something obvious?

1 Answer 1

36

This is specifically documented to be undocumented behaviour.

There is probably something in the way the code is written that will end up picking the same thing every time but the documentation of Enum.ToString states this:

If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return.

(my emphasis)

As mentioned in a comment, a different .NET runtime might return different values, but the whole problem with undocumented behaviour is that it is prone to change for no (seemingly) good reason. It could change depending on the weather, the time, the mood of the programmer, or even in a hotfix to the .NET runtime. You cannot rely on undocumented behavior.

Note that in your example, ToString is exactly what you want to look at since you're printing the value, which will in turn convert it to a string.

If you try to do a comparison, all the enum values with the same underlying numerical value is equivalent and you cannot tell which one you stored in a variable in the first place.

In other words, if you do this:

var x = en.a;

there is no way to afterwards deduce that you wrote en.a and not en.b or en.c as they all compare equal, they all have the same underlying value. Well, short of creating a program that reads its own source.

7
  • At the very least, "every time" very likely won't apply when you run the program on multiple different implementations/versions of .NET.
    – user743382
    Commented Jul 20, 2014 at 16:42
  • That may be true, but however stable or unstable that piece of code is, it is still undocumented. Commented Jul 20, 2014 at 16:43
  • Yes. My issue is not with that, but with your answer: your answer suggests that programs can rely on getting the same enumerator name every time (even though I don't think that's what you meant to say), and the documentation doesn't agree with that suggestion.
    – user743382
    Commented Jul 20, 2014 at 16:46
  • 3
    The reference source does tell you why the output is the way it is. The 4.5.1 implementation is to get the enum names and values, sort them, then do a binary search for the desired value. Thus, it is dependent on sorting and searching implementations, which could easily change. The sort is actually an insertion sort because the common use case is a small number of values that are already sorted. The binary search is just Array.BinarySearch. Commented Jul 20, 2014 at 20:21
  • 2
    To be honest, I'd rather not. You cannot rely on undocumented behavior and that is my answer. I'd rather not actually document the current behavior. It can stay in a comment. Commented Jul 21, 2014 at 7:04

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .