Each of the types you ’ ve seen so far (with the exception of string ) has a clearly defined set of allowed values. Admittedly, this set is so large in types such as double that it can practically be considered a continuum, but it is a fixed set nevertheless. The simplest example of this is the bool type, which can only take one of two values: true or false .
There are many other situations in which you might want to have a variable that can take one of a fixed set of results. For example, you might want to have an orientation type that can store one of the values north , south , east , or west .
In situations like this, enumerations can be very useful. Enumerations do exactly what you want in this orientation type: They allow the definition of a type that can take one of a finite set of values that you supply. What you need to do, then, is create your own enumeration type called orientation that can take one of the four possible values.
Note that there is an additional step involved here — you don ’ t just declare a variable of a given type; you declare and detail a user – defined type and then you declare a variable of this new type.

Defining Enumerations

Enumerations can be defined using the enum keyword as follows:

enum typeName
value1 ,
value2 ,
value3 ,


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
    class Program
        enum orientation : byte
            north = 1,
            south = 2,
            east = 3,
            west = 4
        static void Main(string[] args)
            orientation myDirection = orientation.north;
            Console.WriteLine("myDirection = {0}", myDirection);

You may also like...