Enumerations are special sets of named values that all map to a set of numbers (generally integers). An enumeration improves the code clarity and makes programs easier to maintain. An enumeration can be defined using the Enum keyword. An enum consists of a set of named values called the Enumerator List.
It is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. An enum can also be nested within a class or struct.
By default, in an enum the first enumerator has the integer value 0 and the value of the next enumerator is increased by 1. But we can also assign specific values of each enumerator.
Syntax
Enum <Enum_Name>
{
Enumeration list
}
Example
- enum week
- {
- Sunday,
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday,
- }
Or:
- enum week
- {
- Sunday=1,
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday,
- }
Program 1
- class Program
- {
- static void Main(string[] args)
- {
- int Sunday = 1;
- int Monday = 2;
- int Tuesday = 3;
- int Wednesday = 4;
- int Thursday = 5;
- int Friday = 6;
- int Saturday = 7;
- Console.WriteLine((int)week.Sunday);
- Console.WriteLine((int)week.Monday);
- Console.WriteLine((int)week.Tuesday);
- Console.WriteLine((int)week.Wednesday);
- Console.WriteLine((int)week.Thursday);
- Console.WriteLine((int)week.Friday);
- Console.WriteLine((int)week.Saturday);
- Console.ReadKey();
- }
- }
OutputIn this program we must put the “=” sign and value for every variable that identifies a weekday. An enum provides a convenient way for doing that. We will use an enum to write the preceding program.
- class Program
- {
- enum week
- {
- Sunday = 1,
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday,
- }
- static void Main(string[] args)
- {
- Console.WriteLine((int)week.Sunday);
- Console.WriteLine((int)week.Monday);
- Console.WriteLine((int)week.Tuesday);
- Console.WriteLine((int)week.Wednesday);
- Console.WriteLine((int)week.Thursday);
- Console.WriteLine((int)week.Friday);
- Console.WriteLine((int)week.Saturday);
- Console.ReadKey();
- }
- }
OutputAn enum allows us to create a new data type. In the preceding program you see a data type week created using an enum. An enum can be considered to be a static class. So we access the element within the enum using just the name of the enum week, like
week.Sunday.
The following are some important points related to enums.
- An enum cannot be derived from another enum.
- enum WorkingDay
- {
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday
- }
- enum week:WorkingDay
- {
- Saturday,
- Sunday,
- }
- class Program
- {
- static void Main(string[] args)
- {
-
- }
- }
Output
- An enum cannot be derived by a class (enums are treated as sealed classes).
- enum week
- {
- Sunday,
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday,
-
- }
- class Program:week
- {
- static void Main(string[] args)
- {
-
- }
- }
Output
- We can assign a specific constant value for the enumerator of an enum (not floating point value).
- enum Weight
- {
- Pankaj = 68,
- Lekhraj = 66,
- Raju = 60,
- Rahul = 50
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Weight of {0} = {1}", Weight.Pankaj, (int)Weight.Pankaj);
- Console.WriteLine("Weight of {0} = {1}", Weight.Lekhraj, (int)Weight.Lekhraj);
- Console.WriteLine("Weight of {0} = {1}", Weight.Raju, (int)Weight.Raju);
- Console.WriteLine("Weight of {0} = {1}", Weight.Rahul, (int)Weight.Rahul);
- Console.ReadKey();
- }
- }
Output
- We can assign the same value for the enumerator of an enum.
- enum Weight
- {
- Pankaj = 66,
- Lekhraj = 66,
- Raju = 66,
- Rahul = 50,
- Dheeraj
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Weight of {0} = {1}", Weight.Pankaj, (int)Weight.Pankaj);
- Console.WriteLine("Weight of {0} = {1}", Weight.Lekhraj, (int)Weight.Lekhraj);
- Console.WriteLine("Weight of {0} = {1}", Weight.Raju, (int)Weight.Raju);
- Console.WriteLine("Weight of {0} = {1}", Weight.Rahul, (int)Weight.Rahul);
- Console.WriteLine("Weight of {0} = {1}", Weight.Dheeraj, (int)Weight.Dheeraj);
- Console.ReadKey();
- }
Output
- The value of an enumerator can't change. It behaves like a constant.
- enum Weight
- {
- Pankaj = 68,
- Lekhraj = 66,
- Raju = 60,
- Rahul = 50
- }
- class Program
- {
- static void Main(string[] args)
- {
- Weight.Lekhraj = 60;
- Console.WriteLine("Weight of {0} = {1}", Weight.Pankaj, (int)Weight.Pankaj);
- Console.WriteLine("Weight of {0} = {1}", Weight.Lekhraj, (int)Weight.Lekhraj);
- Console.WriteLine("Weight of {0} = {1}", Weight.Raju, (int)Weight.Raju);
- Console.WriteLine("Weight of {0} = {1}", Weight.Rahul, (int)Weight.Rahul);
- Console.ReadKey();
- }
- }
Output
- By default the type of each enumerator in the enum is an int, but we can specify another integral numeric type using “:” as in the following:
- enum Number : long
- {
- Million = 1000000,
- Billion = 1000000000
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Million = {0}", (long)Number.Million);
- Console.WriteLine("Billion = {0}", (long)Number.Billion);
- Console.ReadKey();
- }
- }