What is C# enum and how to use it?

Defining C# enum

C# enum (enumerator) is a user defined integer type which provides a way for attaching names to numbers, thus increasing the flexibility of code. The enum keyword automatically enumerates a list of worlds by assigning those values 0, 1, 2 and so on. This C# enum facility provides an alternative means of creating ‘constant variable names. The syntax of a C# enum statement is given below:

enum area

{

Length,

Breadth

}

This C# enum initialization can also be written as follows:

enum area {Length, Breadth}

Here Length has the value 0, Breadth has the value 1.

Other examples of C# enum are:

enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

enum position { off, on}

C# enum
C# enum

C# enum initialization

Earlier mentioned that by default C# enum member is set 0 and each of the subsequent member is incremented by one. C# enum members are initialized as follows

enum Color

{

Red = 1,

Blue = 3,

Green = 7,

Yellow = 5

}

It is also possible to have expressions as long as they use the already defined enum members.

Example

enum color

{

Red = 1,

Blue = Red+2,

Green = Red+Blue+3,

Yellow = Blue+5

}

If the declaration of an enumerator member has no initialize, then its value is set implicitly as follows.

·         If it is the first member, its value is zero.

·         Otherwise its value is obtained by adding one to the value of the previous member.

Consider the following C# enum declaration

enum Alphabet

{

A,

B=5,

C,

D=20,

E

}

The member A is set to zero, Since the member B is explicitly given the value 5, the value of C is set to 6 (i.e., 5+1). Similarly, E is set to 21. 

C# enum base types

By default the type of an enumerator is int. However, we can declare explicitly a base type for each C# enum. The valid base types are:

Byte, sbyte, short, ushort, int, uint, long, ulong

Example

enum position

{

Off,

On

}

enum shape : long

{

Circle.

Square =100,

Triangle

}

C# enum type conversion

C# enum types can be converted to their base type and back again with an explicit conversion using a cast.

Example

Enum Values

{

Value0,

Value1,

Value2

Value3

}

…………

…………

Values u1 = (Values) 1;

int a = (int) u1;

…………..

…………..

The exception to this that the literal 0 can be converted to an enum type without a cast.

 i.e., Values u0 = 0; 

Sample program to illustrate the use of C# enum type

using System;

class Area

{

public enum Shape

{

Circle,

Square

}

public void AreaShape(int x, Shape shape)

{

double area;

switch(shape)

{

case Shape.Circle:

area = Math.Pi*x*x;

Console.WriteLine(“Circle Area =”+area);

break;

 case Shape.Square:

area =x*x;

Console.WriteLine(“Square Area= “+area);

Break;

default;

Console.WriteLine(“Invalid input”);

break;
}}

class enumtest

{

public static void main()

{

Area area = new Area();

area.AreaShape(15,AreaShape.Circle);

area.AreaShape(15,AreaShape.Square);

area.AreaShape(15,(Area.Shape)1);

area.AreaShape(15,AreaShape)10);

}}

Output

Circle Area=706.8583

Square Area=225.00000

Square Area=225.00000

Invalid Input

How do you feel about this tutorial ? Is it useful ?

If you think this c# enum tutorial is useful then vote up by clicking the green button below.

Do you have any doubt in c# enum and c# programming feel free to ask me by using the comment column below.


More by this Author


Comments 1 comment

carljsp profile image

carljsp 5 years ago

i was looking for learning c# on internet nice basic start... thanks

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working