Enumerations
Back in the old days of computer mainframes, values were stored with alphanumeric codes that, without some kind of documentation, didn't mean much of anything to anyone but the person who programmed them. For example, an insurance company might rate a car with the code "CC." Any idea what that means? I don't know either.Enumerations make it easier to specify a value that you can spot and understand. In our auto insurance example, CarType.CompactCar is a lot easier to understand than "CC." Listing 2.18 shows how we declare an enumeration.
Listing 2.18. A sample enumeration for car types
C#
VB.NET
enum CarType
{
CompactCar,
Truck,
SportsCar,
Suv
}
Let's apply this concept to something you've probably used before. Every SqlConnection object has a State property that returns a ConnectionState value. Among the more popular values for this property are ConnectionState.Open and ConnectionState.Closed. Beats the heck out of a number or alphanumeric code!You can also specify numeric values for your enumeration values, but they are optional.
Enum CarType
CompactCar
Truck
SportsCar
Suv
End Enum