1

I found some old code in which values in an enum were defined with the same numeric value. Now I am wondering about the behavior. A quick test shows:

Console.WriteLine(Test.A.ToString() + Test.B.ToString() + Test.C.ToString() + Test.D.ToString());

enum Test
{
    A = 1,
    B = 1,
    C = 1,
    D = 1,
}

// output: BBBB

and

Console.WriteLine(Test.A.ToString() + Test.B.ToString() + Test.C.ToString() + Test.D.ToString());

enum Test
{
    A = 1,
    C = 1,
    B = 1,
    D = 1,
}

// output: CCCC

Why do I always get the 2nd name? I would have expected the first or the last one. Is this always the case, or does it depend on some other factors?

cheersmate
  • 2,385
  • 4
  • 19
  • 32
  • 1
    It's not an ID. It's the numerical value. If those are not unique, then the function is not unambiguously reversible. Behavior is not defined for that case, AFAIK. This _may_ make sense in some cases to have the same name for one value, but you need to anticipate, that the other way round may not "work" as expected. – Fildor Jul 13 '23 at 13:15
  • 1
    Just for the notice when i do that on a net 7 console app it prints `AAAA` – jeb Jul 13 '23 at 13:21
  • @jeb Thanks. So there is no reliable / reproducible behavior in this case. That answers my main question. – cheersmate Jul 13 '23 at 13:23

0 Answers0