10

Consider the following code:

using System;

namespace Test
{
    enum Foo
    {
        A = 1,
        B = 1,
        C = 1
    }
    
    public static class Program
    {
        public static void Main()
        {
            Console.WriteLine("{0}, {1}, {2}", Foo.A, Foo.B, Foo.C);
        }
    }
}

Knowing that enums are just integers under the hood, I expected it to be either A, A, A or C, C, C. But surprisingly, it prints out B, B, B! This behaviour appears to be consistent across .NET Framework, .NET Core 3.x and .NET 5.

Why does it choose B?

4
  • can you explain why you expect the output of this constructed hypothetical scenario to be A, A, A or C, C, C? Commented Apr 14, 2021 at 12:23
  • 7
    It's undefined according to the documentation for Enum.GetName(): If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. So it can do what it likes in this regard. As to "why", well I guess you'd have to look at the implementation Commented Apr 14, 2021 at 12:24
  • When you change the order in the enum, it still prints the second one. Commented Apr 14, 2021 at 12:27
  • @FranzGleichmann I expected the enum implementation to contain some sort of value-to-string lookup, which would contain either the first or the last declaration Commented Apr 14, 2021 at 12:27

1 Answer 1

12

It's undefined according to the documentation for Enum.GetName():

If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member.

So it can do what it likes in this regard.

As to why it returns B in your example, we can inspect the implementation of GetEnumName():

public virtual string GetEnumName(object value)
{
    if (value == null)
        throw new ArgumentNullException("value");

    if (!IsEnum)
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
    Contract.EndContractBlock();

    Type valueType = value.GetType();

    if (!(valueType.IsEnum || Type.IsIntegerType(valueType)))
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");

    Array values = GetEnumRawConstantValues();
    int index = BinarySearch(values, value);

    if (index >= 0)
    {
        string[] names = GetEnumNames();
        return names[index];
    }

    return null;
}

Aha! All is explained. To make the lookup faster, they used a binary search. And where is the first place a binary search looks when starting the search? That's right - it starts halfway through the list. And that's why it's finding the B first - after the list is ordered, the B in in the middle.

(Note that the list is ordered by enum value, not enum name, so for your case the list is already ordered since all the values are the same.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.