Enumerations in C#
An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable. For example, assume that you have to define a variable whose value will represent a day of the week. There are only seven meaningful values which that variable will ever store. To define those values, you can use an enumeration type, which is declared by using the enum keyword.
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 ensurethat 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 of an enumerated type, the Visual Studio.NET IDE will, through IntelliSense, pop up a list box of acceptable values to save you some keystrokes and to remind you of what the possible options are.
You can define an enumeration as follows:
public enum TimeOfDay{
Morning = 0,
Afternoon = 1,
Evening = 2 }
Дата добавления: 2015-09-18 | Просмотры: 495 | Нарушение авторских прав
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
|