Skip to content
BENIBIX Learn

Enumeration (Enum)

Development🧩 Programming ConceptsData and Variables

Also known as Enum, Enumerated Type, Enums

A type with a short, fixed list of named choices, like RED, AMBER and GREEN.

Think of it like

It is like a menu with only three drinks. You must pick one of those, nothing else.

Example

Emma's order app has an enum with PAID, SHIPPED and DELIVERED. An order can only ever be one of the three.

Why it matters

Loose text like "shipped" and "Shipped" can slip through as two values. An enum allows exactly one spelling.

Definition

An enum is a type with a fixed list of named choices. A traffic light could be RED, AMBER or GREEN, and nothing else. Code uses the names instead of loose words or numbers. Then the computer can catch a typo, like GRENE, before the code runs. Most languages have enums, including Java, C#, TypeScript and Python.

Example code

python
from enum import Enum

class Light(Enum):
    RED = 1
    AMBER = 2
    GREEN = 3

print(Light.GREEN.name)
# GREEN
Three named choices, and nothing else is allowed.
The fourth card is an enum. It allows RED, AMBER or GREEN and nothing more.

Common mistake

Using plain numbers like 1, 2 and 3 for states. Months later, nobody remembers which number means what.

Key takeaways

  1. 1
    Fixed choices. Only the listed names are allowed.
  2. 2
    Names, not numbers. The code says what it means.
  3. 3
    Typos caught. A wrong name is an error.

Learn it properly