Assigning Values to Members

What we haven’t explained yet is where the (constant) values for the enum’s members come from. Because we didn’t specify any values here, the enum will automatically get consecutive constant values for its members, starting from zero. So the definition of our Color enum is equivalent to the following:

enum Color{    Red   = 0,    Green = Red + 1,    Blue  = Green + 1,}

You can see those values by casting an enum value to the underlying type. Notice that calling ToString on an enum actually prints the symbolic name corresponding to the enum member:

Color red = Color.Red;Console.WriteLine(red);      // Prints "Red".Console.WriteLine((int)red); // Prints "0".

Instead of relying on the automatic ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.