Enumerations, c#

11 12 2007


Enumerations are an alternative to constants, and provide a way of grouping constants together logically. For example, say you’re using a lot of constants like this:

const int Sunday = 1;
const int Monday = 2;
const int Tuesday = 3;
const int Wednesday = 4;
const int Thursday = 5;
const int Friday = 6;
const int Saturday = 7;
System.Console.WriteLine("Sunday is day {0}", Sunday);

This code will give you this output:

Sunday is day 1

However, all the constants you’ve created can be put into an enumeration, which groups them together logically. Here’s how you create an enumeration, using the enum statement:

[attributes] [modifiers] enum identifier [:base-type] {enumerator-list};

Here are the parts of this statement:

  • attributes (optional)— Holds optional declarative meta-information.

  • modifiers (optional)— Optional modifiers that include the new modifier and one of the four access modifiers like public and private.

  • identifier The enumeration name.

  • base-type (Optional)— The underlying type that specifies the storage allocated for each enumerator. It can be any of the integral types except char. The default is int.

  • enumerator-list The enumerators’ identifiers separated by commas, optionally including a value assignment.

Enumerations act like groups of constants, which can be any of the built-in signed or unsigned integer types (such as int, Byte, or UInt64, but not char). You can see an example in ch01_04.cs, in Listing 1.4, which declares an enumeration named Days (unless you declare a type like this, enum Days :uint {...}, the underlying type used for the constant in enumerations is int). (Note that the values in that example ascend smoothly from 1–7, but enumeration values can take any values consistent with their underlying type.)

Listing 1.4 Using an Enumeration (ch01_04.cs)
class ch01_04
  enum Days
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7,

  static void Main()
    System.Console.WriteLine("Sunday is day {0}", (int) Days.Sunday);

The code in Listing 1.4 will also give you this output:

Sunday is day 1

Note that we specify the type for constants in the Days enumeration by prefacing them with the cast (int), which casts the member of the enumeration to its underlying type:

System.Console.WriteLine("Sunday is day {0}", (int) Days.Sunday);

We’ll cover casts in a few pages; the upshot is that if you omit that cast, the value of Days.Sunday would be its symbolic name, “Sunday”, the value of Days.Monday would be “Monday” and so on, not the integers 1, 2, and so on. That’s because in C#, enumerations are formal data types, which means you have to convert them to their underlying types if you want to access the values each enumeration member stands for.

From : Microsoft® Visual C#® .NET 2003 Kick Start




One response

7 01 2008

very interesting.
i’m adding in RSS Reader

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: