Chapter 10

Creating Lists of Items with Enumerations

IN THIS CHAPTER

check Finding real-world examples of enumerations

check Creating and using enumerations

check Using enumerations to define flags

check Using enumerations as parts of switches

To enumerate means to specify individual items as in a list. For example, you might create an enumeration of colors and then list individual colors, such as red, blue, green, and so on. Using enumerations in programming makes sense because you can list individual items as part of a common collection. For example, Colors.Blue would indicate the color blue, and Colors.Red would indicate the color red. Because enumerations are so handy, you see them used all the time in the actual world, which is why you also see them in applications. Code should model the real world to provide useful functionality in an easy-to-understand form.

The enum keyword lets you create enumerations in C#. This chapter begins by discussing basic enum usage but then moves on to some interesting additions you can make. For example, you can use initializers to determine the initial value of each ...

Get C# 7.0 All-in-One For Dummies 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.