CHAPTER 20

image

Enum

An enumeration is a special kind of value type consisting of a list of named constants. To create one, the enum keyword is used followed by a name and a code block, containing a comma-separated list of constant elements.

enum State { Run, Wait, Stop, Offline };

This enumeration type can be used to create variables that can hold these constants. To assign a value to the enum variable, the elements are accessed from the enum as if they were static members of a class.

State s = State.Run;

Enum example

The switch statement provides a good example of when an enumeration can be useful. Compared to using ordinary constants, an enumeration ...

Get C# Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.