Creating and Using Enums
We'll cover the following
In the previous example, we used String
to represent a suit
, but that’s smelly. We don’t need arbitrary values for suit
. We may create a sealed
class Suit
and derived classes for each of the four permissible types. In fact, there can be only four values for suit
. In short, we don’t need classes, we simply need four instances. The enum
class solves that problem elegantly.
Using enums
Here’s an excerpt of code where the suits
properties are converted to use an enum
class Suit
instead of being a String
:
Get hands-on with 1200+ tech skills courses.