An enumerator is a Distinct Type containg a set of Named Constants called Enumerator List. We create an Enumerator with the enum keyword. Enumerator can be of any integer type.
Let's see an example for that
--------------------------------------
enum Gender { Male, Female };
or
enum Gender : int { Male, Female };
--------------------------------------
The above one is and Enumerator with the name Gender. Now we can get the values Male or Female using the enumerator Gender. By default the Value of the First element of the enumerator will be 0 and the rest will be increased by 1.
If you need to use the Enumerator then you can access it using the Gender keyword.
--------------------------------------
Console.WriteLine(Gender.Male);
or
Console.WriteLine(Gender.Female);
--------------------------------------
and if you need to take the value of this Enumerator you can do it like this..
--------------------------------------
int i=(int)Gender.Male;
--------------------------------------
Here in the above code the value will be 0 for the variable i, and if you can take Female the value will be 1.
You can change the values of the enumerator elements in manual.
--------------------------------------
enum Gender { Male=1, Female };
--------------------------------------
Here we forcefully change the value of the first element of the enumerator, so all the rest will be having values after that.
Now let's See and Example for C# Enumerator..
--------------------------------------
using System;
namespace ConsoleApplication1
{
class Program
{
//Creating an Enumerator
enum WeekDays { Sunday=1, Monday,Tuesday, Wednesday,Thursday, Friday, Saturday};
static void Main(string[] args)
{
Console.WriteLine("The value for the week day {0} is {1}",WeekDays.Thursday, (int)WeekDays.Thursday);
}
}
}
--------------------------------------
The output of the Program will be:-
The value for the week day Thursday is 5.