What?
An Enumeration (enum) allows you to specify choices for developers using your class
Why?
You would use an Enum if you have a method or a property which can only accept a limited choice of values.
eg. If you have an application which performs certain actions depending on the day of the week then you can create a method which accepts a Weekdays enum to ensure that only a valid weekday can be entered. If Weekday was a string or an int then it would be possible to add invalid paramaters.
How?
public enum Weekdays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday
}
public void DoSomething()
{
ProcessTodaysFiles(Weekdays.Monday);
}
public void ProcessTodaysFiles(Weekdays weekday)
{
// Switching on an enum
switch (weekday)
{
case Weekdays.Monday:
// Do Mondays work
break;
case Weekdays.Tuesday:
// Do Tuesdays work
break;
case Weekdays.Wednesday:
// Do Wednesdays work
break;
case Weekdays.Thursday:
// Do Thursdays work
break;
case Weekdays.Friday:
// Do Fridays work
break;
default:
break;
}
}