O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required