An enumeration is a user-defined integer type. When you declare an enumeration, you specify a set of acceptable values that instances of that enumeration can contain. Not only that, but you can give the values user-friendly names. If, somewhere in your code, you attempt to assign a value that is not in the acceptable set of values to an instance of that enumeration, the compiler will flag an error. This concept may be new to Visual Basic programmers. C++ does support enumerations (or enums), but C# enumerations are far more powerful than their C++ counterparts.
Creating an enumeration can save you a lot of time and headaches in the long run. At least three benefits exist to using enumerations instead of plain integers:
- As mentioned, enumerations make your code easier to maintain by helping to ensure that your variables are assigned only legitimate, anticipated values.
- Enumerations make your code clearer by allowing you to refer to integer values by descriptive names rather than by obscure “magic” numbers.
- Enumerations make your code easier to type, too. When you go to assign a value to an instance an enumerated type, the Visual Studio .NET IDE will, through IntelliSense, pop up a list box of acceptable values in order to save you some keystrokes and to remind you of what the possible options are.
You can define an enumeration as follows:
In this case, you use an integer value to represent each period of the day in the enumeration. You can now access these values as members of the enumeration. For example, TimeOfDay. Morning will return the value o. You will typically use this enumeration to pass an appropriate value into a method and iterate through the possible values in a switch statement:
The real power of enums in C# is that behind the scenes they are instantiated as structs derived from the base class, Sys tern. Enum.This means it is possible to call methods against them to perform some useful tasks. Note that because of the way the .NET Framework is implemented there is no performance loss associated with treating the enums syntactically as structs. In practice, once your code is compiled, enums will exist as primitive types, just like int and float.
You can retrieve the string representation of an enum as in the following example, using the earlier Time Of Day enum:
This will return the string Afternoon. Alternatively, you can obtain an enum value from a string:
This code snippet illustrates both obtaining an enum value from a string and converting to an integer. To convert from a string, you need to use the static Enum.Parse () method, which, a!’ shown, takes three parameters. The first is the type of enum you want to consider. The syntax is the keyword type of followed by the name of the enum class in brackets, The second parameter is the string to be converted, and the third parameter is-a bool indicating whether you should ignore case when doing the conversion, Finally, note
that Enum.Parse () actually returns an object reference – you need to explicitly convert this to the required enum type (this-is an example of an unboxing operation). For the preceding code, this returns the value 1 as-an object, corresponding to the enum value of Time Of Day. Afternoon, On converting explicitly to an int, this produces the value 1 again.
Other methods on System. Enum do things such as return the number of values in an enum definition or list the names of the values. Full details are in the MSDN documentation.