• Have Any Question?
  • contact@eitworld.com

csharp enumeration





C# Enumeration

 

  • The most basic user defined data type is Enumeration.
  • It arranges a list of items in a sequence and associates a symbolic number to each element.
  • We can say Enumeration is a user defined data type which provides a way for associating a number with a name.
  • In C# we use enum keyword to declare Enumeration.

Declaration of Enumeration

 

    • Declaration of enumeration is done by using enum keyword.
    • The basic sytax to declare a Enumeration data type is as follows:
enum enum_name
{
List of elements
}

    • enum is an keyword of integer data type. Each value in enum corresponds to integer started from 0. The simple example of enumeration is as follows:
enum trafficlight
{
Red,
Green,
Yellow
}

    • According to the above example value stored for these enum elements as:
Red=0
Green=1
Yellow=2

    • You can also assign your own values to elements of enum as given below:
enum trafficlight
{
Red=5,
Green=6,
Yellow=7
}

Here is an example which is sufficient to understand the Enumeration:

 

Example

 

using System;
namespace enumeration
{
class enum1
{

enum trafficlight
{
Red,
Green,
Yellow
}
static void Main(string[] args)
{

int Redvalue=(int)trafficlight.Red;
int Greenvalue=(int)trafficlight.Green;
int Yellowvalue=(int)trafficlight.Yellow;
Console.WriteLine(“Red={0}nGreen={1}nYellow={2}”, Redvalue, Greenvalue, Yellowvalue);
Console.ReadLine();
}
}
}

The Output of the above program will be as follows:

 

Output

 

Red=0
Green=1
Yellow=2



Real Time Web Analytics