You see them fairly often and you see them a lot in the .NET Framework. They do two things.
First they define a type. For example, if you need to let someone specify one of the values North, South, East, West, then you can define an enumeration named CompassDirection. Now I can declare a variable or parameter of that type and you have to use one of the allowed values. For example, if my TurnShip method takes a CompassDirection parameter, you cannot pass it the value 13.
Second, the define the values. In this example, the enumeration defines the values North, South, East, West.
To see why this is useful, suppose you don't have an enumeration. The TurnShip method could take a parameter that is a number (1 = North, 2 = South, etc.). But then you could pass the method the value 5 and confuse it.
Or TurnShip could take a string parameter ("North," "South," etc.). But then you could pass the method the value "down" and again confuse it.
The enumeration gives you strong type checking so Visual Studio can tell at design time whether the value you pass to TurnShip makes sense.
|