enumeration
programming
code best practices
enums in coding
software development

Should an Enum start with a 0 or a 1?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Whether enum values should start at zero or one depends on domain semantics and integration constraints, not style preference alone. Many codebases start at zero because runtimes default enum storage to zero, but external protocols often require one-based codes. The important rule is to assign explicit values and keep them stable.

Why Zero Is Often the Best First Value

Zero is commonly used as a safe sentinel such as Unknown, None, or Unspecified. This is defensive because default-initialized enum fields become zero automatically.

csharp
1public enum JobState
2{
3    Unknown = 0,
4    Queued = 1,
5    Running = 2,
6    Completed = 3
7}

With this design, accidental default values do not pretend to be valid business states.

When Starting at One Is Reasonable

One-based enums make sense when external systems already define codes that way, or when zero is reserved by contract.

csharp
1public enum Priority
2{
3    None = 0,
4    Low = 1,
5    Medium = 2,
6    High = 3
7}

Business values start at one, while zero remains an explicit sentinel.

Treat Numeric Values as Compatibility Contract

If enums are persisted in databases, serialized in APIs, or exchanged with other services, numeric values become part of a public contract. Reordering members without explicit values can silently break compatibility.

csharp
1public enum PaymentStatus
2{
3    Unknown = 0,
4    Pending = 1,
5    Settled = 2,
6    Failed = 3
7}

Always assign explicit numbers for persisted or external enums.

Validate Raw Integers Before Casting

Casting arbitrary integers to enum types can produce invalid states. Validate values at boundaries.

csharp
1public static bool TryParsePriority(int value, out Priority priority)
2{
3    if (Enum.IsDefined(typeof(Priority), value))
4    {
5        priority = (Priority)value;
6        return true;
7    }
8
9    priority = Priority.None;
10    return false;
11}

This pattern is useful for API inputs and database reads.

Flags Enums Follow Different Rules

Bitwise flags enums are not ordinal categories. Use powers of two and keep zero as None.

csharp
1[System.Flags]
2public enum Access
3{
4    None = 0,
5    Read = 1,
6    Write = 2,
7    Execute = 4
8}

Do not use sequential numbers for flags semantics.

Separate Domain Codes From UI Ordering

UI order often changes for usability reasons. Do not encode display ordering in enum numeric values.

csharp
public static readonly IReadOnlyList<Priority> DisplayOrder =
    new[] { Priority.High, Priority.Medium, Priority.Low };

Keep numeric values stable for contracts and use separate mapping for presentation.

Add Stability Tests

Add tests to detect accidental enum value changes during refactors.

csharp
1using Xunit;
2
3public class PriorityTests
4{
5    [Fact]
6    public void PriorityValuesRemainStable()
7    {
8        Assert.Equal(0, (int)Priority.None);
9        Assert.Equal(1, (int)Priority.Low);
10        Assert.Equal(2, (int)Priority.Medium);
11        Assert.Equal(3, (int)Priority.High);
12    }
13}

These tests are low cost and high value in API-driven systems.

API Serialization Guidance

If your API exposes enum values, decide whether to serialize names or integers. Name serialization is more readable, but integer serialization may be required by existing clients. Either way, document mapping clearly and keep backward compatibility.

For example, if integer codes are public, include enum table in API docs and changelog.

Decision Framework

A practical framework:

  1. if default value should mean unknown, start with zero sentinel
  2. if external contract is one-based, mirror contract and reserve zero explicitly
  3. assign explicit numbers always
  4. validate integer inputs at boundaries
  5. add tests for value stability

This keeps behavior predictable across code evolution.

Common Pitfalls

A common pitfall is letting zero map to a valid state unintentionally because of default initialization. Another is relying on implicit enum numbering and then reordering members later. Teams often cast untrusted integers directly to enums without validation. Flags enums are also frequently assigned sequential values incorrectly. UI display order is sometimes tied to numeric enum codes, making presentation changes risky.

Summary

  • Zero is a strong default when used as explicit Unknown or None.
  • One-based enum values are valid when required by external contracts.
  • Assign explicit numeric values for every enum member.
  • Validate raw integer inputs before converting to enums.
  • Use powers of two for flags enums and zero for None.
  • Keep enum contract values separate from presentation ordering logic.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions