FontysICT-sem1

Enum

Definition of Enum

Example

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

The following code is then possible:

Day d;
d = Day.Wednesday;

Another example is the months of the year: January through December.

Why should I use enums?

Here’s an example that demonstrates why: A calendar application where you can add an item to a weekday. Initial approach:

void AddToCalendar(int day, string item)

The days in the week are created as constants. Sunday is 0, Monday is 1, etc. The same applies to the months in the year: January is 0, February is 1, etc.

Now if you make a programming error:

AddToCalendar(February, "whole month of spectacular offers");

This will incorrectly add the item on Monday… Oops.

With enums, the compiler prevents this error because Month and Day are different types:

void AddToCalendar(Day day, string item)

Additional Resources