But if you specify an enum type with three values: red, yellow and green then the compiler will not permit a variable of this type to be assigned any other value. Internally the compiler will assign the values 0,1 and 2 to these colors unless you specify different values for instance 10,15 and 99. It is a very good rule though to always assign a value of 0 to your first enum. If the variable is not explicitly initialized, it defaults to 0 and if this isn't a valid enum value then you have a problem and it may cause an exception.
The code below shows the traffic lights with a color of Off. Note in this example Colors.Yellow has not been given an explicit value, and the compiler uses the next value in the sequence which is 2. When specifying enums, unlike C or C++, you must always prefix the enumerate value with its type. Eg
Colors.Yellow
enum Colors {Off=0, Red = 1, Yellow, Green = 4 };In example two calling WriteLine() on an enum calls its .ToString() method. When you run this program it outputs
static void Main(string[] args)
{
Colors Lights = Colors.Yellow;
Console.WriteLine("Lights is {0} {1}",Lights,(int)Lights) ;
Console.ReadLine() ;
}
Yellow 2
On the next page : Floating Point Numbers

