I am reading about enumerations in a book, and the following code example was given:
namespace FunWithEnums
{
enum EmpType : byte
{
Manager = 10,
Grunt = 1,
Contractor = 100,
VicePresident = 9
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("**** Fun with Enums *****");
EmpType emp = EmpType.Contractor;
EvaluateEnum(emp);
Console.ReadLine();
}
static void EvaluateEnum(System.Enum e)
{
Array enumData = Enum.GetValues(e.GetType());
for (int i =0; i < enumData.Length; i++)
{
Console.WriteLine("Name: {0}, Value: {0:D}", enumData.GetValue(i));
}
Console.WriteLine();
}
}
I'm very confused as to what is being printed out in the forloop. The output is
Name: Grunt, Value: 1
Name: VicePresident, Value: 9
Name: Manager, Value: 10
Name: Contractor, Value: 100
But how does it get both the name AND the value of each enumerated element? According to the Microsoft documentation, Array enumData = Enum.GetValues(e.GetType());
should only return " an array of the values of the constants in a specified enumeration." I assume that by constants, it is referring to "Manager", "Grunt", "Contractor", "VicePresident", as opposed to 10, 1, 100 and 9. So why is it returning each pair instead of just the name of the employee type? Also, the output of the following code Console.WriteLine(enumData.GetValue(1));
returns just one value, which is "VicePresident" and doesn't return the number 9 like it does in the forloop. Why is this?