CHAPTER 20

image

Enumerations

Enumerations are useful when a value in the program can have only a specific set of values. An enumeration might be used where a control supports only four colors or for a network package that supports only two protocols.

A Line-Style Enumeration

In the following example, a line-drawing class uses an enumeration to declare the styles of lines it can draw:

using System;public class Draw{    public enum LineStyle    {       Solid,       Dotted,       DotDash,       // trailing comma is optional    }    public void DrawLine(int x1, int y1, int x2, int y2, LineStyle lineStyle)    {       switch (lineStyle)       {             ...

Get A Programmer's Guide to C# 5.0, 4th Edition 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.