Search This Blog

Sunday, August 23, 2009

Enumaretion with C#

Enums Defined

Enums are lists of strongly typed constants with members that has symbolic names, corresponding integral values.
The System.Enum .NET Framework Class Library type is the base class of enum types and contains methods that allow you to work with enums in different ways, such as working with a list of names or values, converting from value to name, and converting from name to value.
Enum base types can be changed and member values can be specified.
Enums make working with strongly typed constants via symbolic names easy.
Enums are value types, which mean they contain their own value, can't inherit or be inherited from, and assignment copies the value of one enum to another.

Simply if a single number needs a definition to make it easier to read then weuse a constant like

public const int myVar = 0;
if more than one related number needs a definition, then we can use an Enumeration


Difference Between ‘Enum’ and ‘enum’

The C# type, enum, inherits the Base Class Library type, Enum. Use the C# type, enum, to define new enums and use Enum, to implement static enum methods.

Creating an Enum
Declaration
[attributes] [modifiers] enum identifier [:base-type]
{
enumerator-list [,]
}

· The attributes is optional and is used to hold additional declarative information.
The modifier is optional. The allowed modifiers are new, public, protected, internal and private.
The keyword enum must be followed by an identifier that names the enum.
The base-type of an enumeration may be one of the following; byte, sbyte, short, ushort, int, uint, long or ulong. If no base-type is declared, than the default of int is used.
The enumerator-list contains the identifiers which are separated by commas
The first enumerator begins at zero by default and each enumerator following is increased by 1. This can be overridden so that each member of the enumerator-list contains its own unrelated value.
Two items in an enumeration can hold the same value, however, this will cause problems if you use an automated switch statement where all elements are added.

Example
using System;
using System.Collections.Generic;
using System.Text;

// declares the enum

public enum Universe{

Planet,

Stars,

Satellite

}

// demonstrates how to use the enum

class CreateEnum{

static void Main()

{

// create and initialize

// instance of enum type

Universe varUniverse = Universe.Planet;

// make decision based

// on enum value

switch (varUniverse)

{

case varUniverse.Planet:

Console.WriteLine("Enum Picks the planet.");

break;

case varUniverse.Stars:

Console.WriteLine("Enum pics the Stars.");

break;

case varUniverse.Satellite:

Console.WriteLine("Enum picks the Satellite.");

break;

}

Console.ReadLine();

}

}
Here I have declared an enum by using the enum keyword having a type identifier (Universe).The enum contains a comma separated list of values enclosed within curly braces.
This enum is of type Universe and we use it to declare the varUniverse variable in the Main method. Since an enum is a value type, we can assign a value (Universe.Planet) to it directly, similar to the simple types such as int or double.

Using Enums
Enum can be customized by changing its base type and its member values. By default, the list type of an enum is int.During enum declaration you can change this default by the base types include byte, sbyte, short, ushort, int, uint, long, and ulong.
Another modification you can make to an enum is to set the value of any enum member. By default, the first member of an enum takes zero.You can change it to any number you want.Two or more list member can have the same value.But it would some logical error through your programme thereafter.
using System;

// declares the enum

public enum Universe

{

Planet=1,

Stars,

Satellite

}

// demonstrates how to use the enum
class CreateEnum{
static void Main()
{
// create and initialize
// instance of enum type
Universe varUniverse = Universe.Planet;
// make decision based
// on enum value
switch (varUniverse)
{
case varUniverse.Planet:
Console.WriteLine("Enum Picks the planet.");
break;
case varUniverse.Stars:
Console.WriteLine("Enum pics the Stars.");
break;
case varUniverse.Satellite:
Console.WriteLine("Enum picks the Satellite.");
break;
}
Console.ReadLine();
}
}

The first member of the Universe enum, Planet, has its value changed to 1. You are restricted from creating forward references, circular references, and duplicate references in enum members.

As the default value of Planet is changed to 1, so the rest values are Stars=2, Satellite=3

Changing Enum Base Type

using System;
// declares the enum
public enum Universe : byte
{
Planet=1,
Stars,
Satellite
}
// demonstrates how to use the enumclass CreateEnum{ static void Main() { // create and initialize // instance of enum type Universe varUniverse = Universe.Planet; // make decision based // on enum value switch (varUniverse) { case varUniverse.Planet: Console.WriteLine("Enum Picks the planet."); break; case varUniverse.Stars: Console.WriteLine("Enum pics the Stars."); break; case varUniverse.Satellite: Console.WriteLine("Enum picks the Satellite."); break; } Console.ReadLine(); }}

2 comments:

Sushanta said...

good ..i got something from it

Peter Mc said...

Please post something brief on enum base type changes and its uses.where can i use enum ?