My shared items


Thursday, August 9, 2007

enum at C# Online.NET (CSharp-Online.NET)

enum at C# Online.NET (CSharp-Online.NET)


The Basics

An enum is a value type with a set of related named constants often referred to as an enumerator list. They allow code to look a lot cleaner and easier to read by getting rid of "magic numbers", that is to say, they get rid of numbers which have a purpose within a module of code, but make the code harder to read. If a single number needs a definition to make it easier to read then use a constant as shown below.

public const int North = 0;

However, if more than one related number needs a definition, then use an enumeration.


An enum is declared as follows:

[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 (optionally including a value assignment).

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 (see second example below). 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 1 – A simple enumeration

An application may need to work with directions. Instead of using four numbers to represent the major directions, an enumeration works better. The numbers then become irrespective as the directions are named.

enum Direction

Example 2 – Number specific

Now consider the same enumeration, but with each direction holding the value clockwise that they are from North.

enum Direction
North = 0,
East = 90,
South = 180,
West = 270

We would use the enumeration in the same manner, but the values have different meanings.


To get the underlying value of an item in an enumerator-list, you need to have an explicit cast to convert it from the enum type to its integral type. The following code displays this and makes use of the enum in example 2.

static void Main(string[] args)
Console.WriteLine("Direction Enum Members by Name:");

// get a list of member names from Direction enum,
// figure out the numeric value, and display
foreach (string direction in Enum.GetNames(typeof(Direction)))
Console.WriteLine("Direction Member: {0}\n Value: {1}",
direction, (int)Enum.Parse(typeof(Direction), direction));

And here is the output

Direction Enum Members by Name:
Direction Member: North
Value: 0
Direction Member: East
Value: 90
Direction Member: South
Value: 180
Direction Member: West
Value: 270

Some useful methods

  • Enum.GetName - retrieves the name of the constant in the enumeration that has a specific value.
  • Enum.GetNames - retrieves an array of the names of the constants in the enumeration.
  • Enum.GetValues - retrieves an array of the values of the constants in the enumeration.
  • Enum.IsDefined - returns an indication whether a constant with a specified value exists in a specified enumeration.

MSDN references

No comments: