top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is enum in C#?

0 votes
245 views
What is enum in C#?
posted Mar 28, 2017 by Pooja Bhanout

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Enumerations or enums is a set of named integer constants. In C# enums are User-Defined Value Types and enum constants must be integral numeric values. Its index starts from 0 and the value of each successive enumerator is increased by 1. For example consider the following enumeration. Here enumeration Sat is 0, Sun is 1, and Mon is 2

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

But we can use initializers to override the default value. Consider the following example.

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration the sequence of elements is forced to start from 1 instead of 0. But it is recommended to set its value 0.

Declaration of enums
Enumerated type is declared using the enum keyword. The general syntax for declaring an enumeration is:

enum 
{
    enumeration list
};

Where,
The enumName specifies the enumeration type name.
The enumeration list is a comma-separated list of identifiers.

enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

Example of enum
Example of enum in C#:

enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

static void Main(string[] args)
{
    Console.WriteLine("Sunday: {0}", (int)Days.Sun);
    Console.WriteLine("Friday: {0}", (int)Days.Fri);
    Console.ReadKey();
}

Output
If we run the above code in a Console Application the output will be:

Sunday: 0
Friday: 5

answer Mar 29, 2017 by Shivaranjini
...