Saturday, October 20, 2018

C# Enum(Enumeration) with Example

C# Enumeration

An enumeration is used in any programming language to define a constant set of values. For example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the keyword 'enum'.
Let's see an example of how we can use the 'enum' keyword.
In our example, we will define an enumeration called days, which will be used to store the days of the week. For each example, we will modify just the main function in our Program.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
class Program
namespace DemoApplication { {
static void Main(string[] args)
enum Days{Sun,Mon,tue,Wed,thu,Fri,Sat}; {
Console.Write(Days.Sun); Console.ReadKey(); }

Code Explanation:-

  1. The 'enum' data type is specified to declare an enumeration. The name of the enumeration is Days. All the days of the week are specified as values of the enumeration.
  2. Finally the console.write function is used to display one of the values of the enumeration.
If the above code is entered properly and the program is executed successfully, the following output will be displayed.


From the output, you can see that the 'Sun' value of the enumeration is displayed in the console.

No comments:

Post a Comment