Enumerations

Enums (short for Enumerations) are a set of named numeric constants. They are useful when some value in a program can have a specific set of values. Suppose we want our program to accept only three values such as Pizza, Pasta and Spaghetti as the food type. We can easily enforce this by specifying an enum (foodType), which consists of the specified values, and write a method that accepts only this enum as a parameter. This is shown in Example 17.

Example 17:

Enumerators Example 17

Enumerations in C#, as in the case of C/C++, have numbers associated with the values. By default, the first element of the enum is assigned a value of 0 and is incremented for each subsequent enum element. However, if you wish to override this and specify your own values you can very well do so at the initialization stage. Thus, Example 17 cab be modified as shown in Example 18.

Example 18:

Enumerators Example 18

Leave a comment