Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Enumerations in C#

An enumeration is used to represent a set of related values.
Example: Up-Down, North-South-East-West

An enumeration is defined using the enum keyword and a code block in which the various values are defined.
Here’s a simple example:
  1. enum color
  2. {
  3. Red,yellow
  4. }

Once defined, an enumeration value can be used by specifying the enumeration type, a dot, and then the specific name desired:

  1. color rainbow = color.Red;


The names within an enumeration are actually numeric values. By default, they are integers, whose value begins at zero.
You can modify both the type of storage used for these values and the values associated with a particular name.
Here’s an example, where a short is used to hold different coin values:
  1. enum Coin: short{
  2. Penny = 1, Nickel = 5, Dime = 10, Quarter = 25
  3. }

Then, the names can be cast to their implementing value type:
  1. short change = (short) (Coin.Penny + Coin.Quarter);


This will result in the value of change being 26.

It is also possible to do bitwise operations on enumerations that are given compatible:
  1. enum Flavor{
  2. Vanilla = 1, Chocolate = 2, Strawberry = 4, Coffee = 8
  3. }

  1. Flavor conePref = Flavor.Vanilla | Flavor.Coffee;



..