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

Get The C# Programming Language (Covering C# 4.0), Fourth Edition now with O’Reilly online learning.

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