What determines the result of casting an int to enumerate when multiple names have the same value?

Denial of responsibility. I do not condone this listing. This leads to confusion and a tendency to make mistakes. However, I am curious that I discovered an irregularity.

I used the following code ( fiddle here ):

enum Anims{ 
  Donkey = 1, Wonkey = 1, 
  Monkey = 2, Oinky  =2, 
  A = 3, B = 3, C = 3, bb = 4, aa = 4
}

foreach(Anims anim in Enum.GetValues(typeof(Anims)))
  Console.WriteLine((Anims)((int)anim));

The result that I expected was either that the declared values ​​would be presented. Perhaps the values ​​that were assigned last. Nothing of the kind happened. I also looked at the alphabetical order of the output. No ...

What is an algorithm that determines which integer will be enclosed in an enumeration if there are several values ​​corresponding to it?

Conclusion:

Wonkey
Wonkey
Monkey
Monkey



bb
bb

+4

Source: https://habr.com/ru/post/1677251/


All Articles