Enumerations

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 ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.