Enum - is a distinct type consisting of a set of named constants called the enumerator list.
Example - of how to enumerate an
Enum in c#.
In the below example you will see the keyword
var, which is a new keyword in C# 3.0 (.NET 3.5) that says I don't feel like typing out the type of this variable (or don't know it), I want the compiler to infer it for me.
There are two functions in the Enum example below -
- PrintEnumNames() will display all the names of the animals - Horse Cow Dog Cat
- PrintEnumValues() will display all the values of the animals - 1 2 3 4
public enum Animals
{
Horse = 1,
Cow = 2,
Dog = 3,
Cat = 4
}
public void PrintEnumNames()
{
foreach(string name in Enum.GetNames(typeof(Animals)))
{
Console.WriteLine(name);
}
}
public void PrintEnumValues()
{
foreach(var animal in Enum.GetValues(typeof(Animals)))
{
Console.WriteLine(animal.ToString());
}
}