Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Enumeration in C# (OOP’s) – Part 21

Posted by Ravi Varma Thumati on July 14, 2009

Introduction

Enumeration is about a distinct type consisting of a set of named constants called the “Enumerator List”.

We can declare the enumeration by using the keyword “enum”. By default, the first value of the first enumerator is [0], and the value of the successive enumerators is increased by [1].

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace EnumeratorList

{

class Program

{

enum letter { A, B, C, D, E };

static void Main(string[] args)

{

//get the elements in the list

Console.WriteLine(letter.A);

Console.WriteLine(letter.B);

Console.WriteLine(letter.C);

Console.WriteLine(letter.D);

Console.WriteLine(letter.E);

Console.WriteLine(“\n\n”);

//do an explicit cast

//to get the corresponding values for the

//enumerator list elements

int a = (int)letter.A;

int b = (int)letter.B;

int c = (int)letter.C;

int d = (int)letter.D;

int e = (int)letter.E;

//print values

Console.WriteLine(“the value of enumerator A is : {0}”, a);

Console.WriteLine(“the value of enumerator B is : {0}”, b);

Console.WriteLine(“the value of enumerator C is : {0}”, c);

Console.WriteLine(“the value of enumerator D is : {0}”, d);

Console.WriteLine(“the value of enumerator E is : {0}”, e);

Console.WriteLine(“\n\n”);

}

}

}

The Output of this example will be as the following:

enumeration1

We can override the default value of the first enumerator, and this can be done by passing a value to the first enumerator. As the following example;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace EnumeratorList

{

class Program

{

enum letter { A=10, B, C, D, E };

static void Main(string[] args)

{

//get the first element

Console.WriteLine(letter.A);

Console.WriteLine(letter.B);

Console.WriteLine(letter.C);

Console.WriteLine(letter.D);

Console.WriteLine(letter.E);

Console.WriteLine(“\n\n”);

//do an explicit cast

//to get the corresponding values for the

//enumerator list elements

int a = (int)letter.A;

int b = (int)letter.B;

٢٢

int c = (int)letter.C;

int d = (int)letter.D;

int e = (int)letter.E;

//print values

Console.WriteLine(“the value of enumerator A is : {0}”, a);

Console.WriteLine(“the value of enumerator B is : {0}”, b);

Console.WriteLine(“the value of enumerator C is : {0}”, c);

Console.WriteLine(“the value of enumerator D is : {0}”, d);

Console.WriteLine(“the value of enumerator E is : {0}”, e);

Console.WriteLine(“\n\n”);

}

}

}

The Output of this example will be as the following:

enumeration2

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace EnumeratorList

{

public enum DayOfWeek

{

Sunday = 0,

Monday = 1,

Tuesday = 2,

Wednesday = 3,

Thursday = 4,

Friday = 5,

Saturday = 6

}

class Program

{

static void Main()

{

DayOfWeek day = DayOfWeek.Thursday;

int x = (int)DayOfWeek.Thursday;

٢٣

System.Console.WriteLine(“today is : {0}, \n\nand the value of the day

in the enumerator list is: {1}”, day, x);

Console.WriteLine(“\n\n”);

}

}

}

The Output of this example will be as the following:

enumeration3

Underlying Types:

Every enumerator type has an underlying type which can be any integral type except “char”, and the default underlying type is “int”. We can see this in the cast operation;

In the previous two examples, we saw that, to get the value of the enumerator elements in the enumerator list, we convert each enumerator to the default underlying type which is “int”. The importance of using the underlying types is to specify how much storage is allocated for each enumerator in the memory.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: