Enum Basics
In C# language, enum is the data type.
Enum is defined according to-
- enum Month {Jan,Feb,March,April}
-
- Console.WriteLine("The Value of Enum is {0}",Month.Jan);
-
Do You Know
- Even though, you are assigning the names to the enum member list; the compiler actually assigns an integer value to the member of the list starting with zero(0) and increment each for the successive members.
If you want to know about assigning integer value of particular enum member in the list, you need to type cast it to the integer, using Convert.Toint32() method.
For eg :- Finding integer value.
- Console.WriteLine(Convert.toInt32(Month.Jan));
Output: 0
You can also give the custom enum list value programmatically.
For Example
- enum Month {Jan=2,Feb=3,March=5};
- Console.writeline(Convert.toInt32(Month.Jan));
Output: 2.
By default, the compiler initializes int value to the list of Enum starting form zero(0). We can make change in the data type of enum as well through programmatically, changing default data type of enum form int to byte type.
- enum Month:byte {Jan=2,Feb=3,March=5};
- Console.writeline(sizeof(Month));
Output: 1 ///Try it by replacing byte to int and you get output as 4.