The enum I’ve created looks like this:
enum MonthOfTheYear : byte
{
January,
February,
March,
April,
May,
June,
July = 0,
August,
September,
October,
November,
December
}
As you can see, July has an initializer of 0.
This has some interesting (side) effects: there seems to be “pairing” of integer values. February ànd August now have values of 1, March ànd September have 2 etc.:
MonthOfTheYear theMonth = MonthOfTheYear.February;
Console.WriteLine(theMonth + " has integer value of " + (int)theMonth);
and
MonthOfTheYear theMonth = MonthOfTheYear.August;
Console.WriteLine(theMonth + " has integer value of " + (int)theMonth);
clearly show this. So far, weird as I find that, I’m willing to go along.
EDIT: I get that assigning July 0 makes the indices start over. I DON’T get why they can co-exist within the same enum.
BUT! IF I then loop through the enum and output all the underlying integer values, weirdness ensues.
MonthOfTheYear theMonth = MonthOfTheYear.January;
for (int i = 0; i < 12; i++)
{
Console.WriteLine(theMonth + " has integer value of " + (int)theMonth++);
}
outputs
July has integer value of 0
February has integer value of 1
September has integer value of 2
April has integer value of 3
May has integer value of 4
June has integer value of 5
6 has integer value of 6
7 has integer value of 7
8 has integer value of 8
9 has integer value of 9
10 has integer value of 10
11 has integer value of 11
I was hoping someone could explain to me what’s going on behind the scenes, because the integer values are successive, so I’m thinking this is outputting as expected but I’m not seeing it as of yet.
http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx
So, to sum it up, when you have multiple members with the same value, the name you get for a particular value is any of the members with that value.