Home / Definitions / Enumerated Type

Enumerated Type

Abby Braden
Last Updated May 24, 2021 8:02 am

In programming, an enum, short for enumerated type, is a user-defined data type consisting of a set of named values called enumerators. Instead of using an integer to represent a set of values, a type with a restricted set of values is used instead. Using enums increases the level of abstraction and lets the programmer focus on the value’s meaning instead of its storage and accessibility. This, in turn, reduces bugs.

Think of the four suits in a deck of playing cards clubs, diamonds, hearts, and spades as four enumerators belonging to an enumerated type named suit. If a variable V is declared having suit as its data type, one can assign any one of those four values (clubs, diamonds, hearts, and spades) to it.

Values and variables of an enum are implemented as fixed-length bit strings that are compatible in format and size with some integer type. In type theory, enums are considered as tagged unions of unit types. Programming languages that use enums include Pascal, Java, and C#.

Enum benefits

Enums provide the following benefits:

  • They are a constant rather than a number, increasing readability of the source code.
  • They provide compile-time type safety and prevent comparing constants in different enums. A function argument, return type, class member, or local variable can be declared as an enum type and the compiler will enforce type safety.
  • Enums group things into a set.
  • In Java, enumeration can be used as a singleton. A single element enum type is considered one of the best ways to implement a singleton.