Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Enum

Перечисление

A type that gives names to a predefined set of choices. For example, a delivery method can be pickup, standard shipping, or express shipping rather than an arbitrary string.

Also known as: enum

What an enum represents

An enum (short for enumeration) is useful when a program needs to distinguish between a known set of alternatives. A checkout function that takes a Delivery value tells callers more than one that takes an integer: Delivery::Pickup is meaningful without a comment explaining what 0 stands for. Unlike a group of integer constants, an enum can give those choices a type of their own.

Enums work well for choices owned by the program, such as sort direction or the state of a background job. They are a poor fit for a catalog that customers or administrators need to edit without deploying code. In that case, a database table or another data-driven representation is usually a better model.

How variants work

The details depend on the language. In C#, an enum has an underlying integer type, and an explicit cast can produce an enum value with no corresponding named member. In Rust, enum variants can carry different data. That lets you express not just which option was selected, but also what information belongs to that option.

Example in Rust

enum Delivery {
    Pickup,
    Courier { distance_km: u32 },
}

fn fee_cents(delivery: Delivery) -> u64 {
    match delivery {
        Delivery::Pickup => 0,
        Delivery::Courier { distance_km } => {
            300 + 50 * u64::from(distance_km)
        }
    }
}

A courier delivery requires a distance; pickup has no distance field at all. A single struct with a delivery type and an optional distance would also allow combinations such as a courier delivery with no distance. The enum rules out that combination. Rust also checks that the match covers every variant. Add a Locker variant and this function will no longer compile until the new case is covered. A catch-all arm (_) can be intentional, but it means the compiler won't flag a newly added variant as missing here.

Common mistakes

Treating an internal enum as a wire format. If you save a member’s position in a database, reordering the declaration can change how stored records are read. If you send member names over an API, a seemingly harmless rename can break clients. Define stable protocol values and decide how to handle unknown input before converting it to your internal type.

Using an exclusive choice for independent options. A delivery uses one method, but a notification can go to both email and SMS. For the latter, use a set of enum values or a flags representation if the language supports it. A single ordinary enum value doesn't model an arbitrary combination.

How it comes up in interviews

An interviewer might ask you to replace a status string with an enum, then ask what happens when another service starts sending a status your code doesn't recognize. Separate the two checks: exhaustive branching helps you handle known variants in your own code; parsing and validation determine what happens to unfamiliar values at the system boundary.