Enum-Enumerations - High-Flying C# Coding Tips

Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!


Enum - Enumerations

Enumerations provide a way to create a set of variables which have a matching string name and object value (int by default) for each variable, each one of these is known as an enumerator.

Enumerations are created using the enum keyword.  You might have a range of keywords you want to use which represent something, for example vegetables.  Here, you may have an object, or setting, or similar which could be one of several different vegetables, such as a carrot, a spinach, a turnip, an onion or a cauliflower.   In your code, you might want to say that something is a carrot, or check whether something has been set to a carrot.  Using the enum keyword, we can create an enumeration of these vegetables using the following code:

// Create the enumeration of vegetables
public enum Vegetable { Carrot = 0, Spinach = 1, Turnip = 2, Onion = 3, Cauliflower = 4 };

In the above code we have created an enumeration of vegetables, in reality you may have status codes or something else.  In the above example we have created the enumeration and specified the integer value for each vegetable.  You can however leave out the integer value and then the set of values will start with 0 by default, as shown in the next example.

// Create the enumeration of vegetables
public enum Fruit { Apple, Orange, Banana };

Comparing Enums

So, if we want to check to see if an enumeration is set to a particular value, rather than checking to see if it's an integer value, we can check to see if it was one of the enumerators by it's string name value, as shown next.  The syntax certainly makes more sense than just comparing integer values, so the code becomes easier to read and easier to maintain.

Vegetable someVegetable = Vegetable.Carrot;

if (someVegetable == Vegetable.Carrot)
    // our enumeration someVegetable is a Carrot

Casting Enums to Strings and Integers

We can take the string name value or the integer value if we want to, by calling .ToString() or casting the enum to an integer, as shown below:

// get the string value from the enumeration
string typeOfVegetable = someVegetable.ToString(); // typeOfVegetable = "Carrot"

// get the integer value from the enumeration
int vegetableIntegerValue = (int)someVegetable; // vegetableIntegerValue = 0

Fruit newFruit = Fruit.Apple;
int fruitInteger = (int)newFruit; // fruitInteger = 0

Enum.Parse - Creating an Enum from a String or Integer

If we have a string which contains the name of the enumerator, for instance say that we have stored the enumerator value of an enumeration into a database, and we want to create an enum from that value, then we can call Enum.Parse as shown next:

// cast a string value to the enumeration
string onionString = "Onion";
Vegetable newVegetable = (Vegetable)Enum.Parse(typeof(Vegetable), onionString);

if (newVegetable == Vegetable.Onion)
    // then the string was an Onion

We can do the same if we have the integer value as well:

string spinachIntegerString = "3";
Vegetable secondNewVegetable = (Vegetable)Enum.Parse(typeof(Vegetable), spinachIntegerString);

Different types of Enumerations

Finally, enumerations don't have to be just ints, they can be made from a byte, sbyte, short, ushort, unit, long, or ulong as well.

public enum Distances : long { Start = 0, Midpoint = 2000000000L, Finish = 8000000000L };


   long finishLine = (long)Distances.Finish;