Hello Friends, In this session we are going to discuss Enumeration in C#. We can see Enumeration as an alternate of constants in C#. Let’s discuss Enumeration with an example.

Introduction:

Enumeration is a user-defined Integer type that contains distinct value type. When we talked about Enumeration we talked about set of named Integer constants. The keyword enum is used to declare an enumerated type.

Enumeration

 

The syntax for declaring Enumeration in C#.

enum enumname {enumerationlist};

As you can see in the above syntax: enum keyword is used to declare an enumerated type. “enumname” specifies the name of the enum.” Enumerationlist” is the comma-separated list of named Integer types.

  • “enum” keyword is used to declare an enumerated type.
  • “enumname” specifies the name of the enum.” Enumerationlist” is the comma-separated list of named Integer types.
  • ” Enumerationlist” is the comma-separated list of named Integer types.

Since it is a set of named Integer constants, it provides an alternate way to Constants. Let’s understand how enumeration provides a way to us to replace constants.

Consider two constant variable in C#.

constant int Rate1=100;

constant int Rate2=200;

If you wish to add some more constants to the list such as:

constant int Rate3=300;

constant int Rate4=400;

constant int Rate5=500;

This is somewhere an awkward process and there is logical reason to declare various constants. This problem is overcome by Enumeration. We can achieve the same thing using enumeration.

enum RateEnum

{

Rate1=100,

Rate2=200,

Rate3=300,

Rate4=400,

Rate5=500

}

An example of Enumeration in C#:

Example 1:

Open Visual Studio and create a Console application project “EnumerationDemo”.

Write the following code in Program.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumerationDemo
{
 class Program
 {
 enum Weekdays 
 {
 Monday=1,
 Tuesday=2,
 Wednesday=3,
 Thursday=4,
 Friday=5,
 Saturday=6,
 Sunday=7,
 } 
 static void Main(string[] args)
 {
 Console.WriteLine("Weekdays position");
 Console.WriteLine("Sunday {0}", (int) Weekdays.Sunday);
 Console.WriteLine("Monday {0}", (int) Weekdays.Monday);
 Console.WriteLine("Tuesday {0}", Weekdays.Tuesday);
 Console.WriteLine("Wednesday {0}", (int)Weekdays.Wednesday);
 Console.WriteLine("Thursday {0}", (int)Weekdays.Thursday);
 Console.WriteLine("Friday {0}", (int)Weekdays.Friday);
 Console.WriteLine("Saturday {0}", (int)Weekdays.Saturday);
 Console.ReadLine();


 }
 }
}

Run the application and look at the output screen.

Enumeration

Download the complete source code for the application here.

Create another example to get a better understanding of Enumeration:

Example2:

Create a Console Application project called “EnumerationDemo2”.

Enumeration

Write the following code in Program.cs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumerationDemo2
{
 class Program
 {
 public enum TimeOfDay
 {
 Morning = 0,
 Afternoon = 1,
 Evening = 2
 }

static void Main(string[] args)
 {
 GreetingMessage(TimeOfDay.Morning);
 GreetingMessage(TimeOfDay.Evening);
 GreetingMessage(TimeOfDay.Afternoon);
 Console.ReadKey();


 }

static void GreetingMessage(TimeOfDay timeOfDay)
 {
 switch (timeOfDay)
 {
 case TimeOfDay.Morning:
 Console.WriteLine("Good morning!");
 break;
 case TimeOfDay.Afternoon:
 Console.WriteLine("Good afternoon!");
 break;
 case TimeOfDay.Evening:
 Console.WriteLine("Good evening!");
 break;
 default:
 Console.WriteLine("Hello!");
 break;
 }
 }

}
}

Run the application and look at the output screen.

Enumeration

Download the complete source code for Example 2 from here.

Hope you understand what is enumeration in C#. Your feedback is valuable for me.

Read More:

Thank You.

One Thought to “Enumeration in C#”

  1. Very good blog,thank you very much for your effort in writing the posts.

     

Leave a Comment