O'Reilly logo

The C# Programming Language (Covering C# 4.0), Fourth Edition by Peter Golde, Scott Wiltamuth, Mads Torgersen, Anders Hejlsberg

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

14. Enums

An enum type is a distinct value type (§4.1) that declares a set of named constants.

Jon Skeet

Notably, these “named constants” are effectively just numbers. They cannot express behavior, contrary to almost everything else in C#. This is one of the very few areas where Java is more expressive than C#. In Java, enums have a lot more power: An enum can declare methods and then override them for specific values, for example. While it’s possible to emulate some of the features of Java enums within C#, language (and framework) support in a future version would be extremely welcome.

The example

enum Color{    Red,    Green,    Blue}

declares an enum type named Color with members Red, Green, and Blue.

14.1 Enum Declarations

An enum declaration ...

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