programming
enums
arrays
coding tips
software development

How to get all enum values as an array

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

Getting all enum values as an array is usually straightforward, but the exact API depends on the language. The common idea is the same everywhere: enums define a fixed set of named constants, and the language or runtime usually provides a built-in way to enumerate them in declaration order.

Java: Use values()

In Java, every enum type gets a compiler-generated values() method.

java
1enum Direction {
2    NORTH, SOUTH, EAST, WEST
3}
4
5public class Demo {
6    public static void main(String[] args) {
7        Direction[] values = Direction.values();
8        for (Direction d : values) {
9            System.out.println(d);
10        }
11    }
12}

This returns an array in declaration order.

C#: Use Enum.GetValues

In C#, use Enum.GetValues and cast the result to the enum type.

csharp
1using System;
2using System.Linq;
3
4enum Direction
5{
6    North,
7    South,
8    East,
9    West
10}
11
12class Program
13{
14    static void Main()
15    {
16        Direction[] values = Enum.GetValues(typeof(Direction))
17                                 .Cast<Direction>()
18                                 .ToArray();
19
20        foreach (var d in values)
21        {
22            Console.WriteLine(d);
23        }
24    }
25}

In newer C# versions, a generic form is available too:

csharp
Direction[] values = Enum.GetValues<Direction>();

That is the cleanest version when supported.

Python: Convert the Enum to a List

Python's Enum type is iterable.

python
1from enum import Enum
2
3class Direction(Enum):
4    NORTH = 1
5    SOUTH = 2
6    EAST = 3
7    WEST = 4
8
9values = list(Direction)
10print(values)

If you want only the raw member values rather than enum objects:

python
raw_values = [item.value for item in Direction]
print(raw_values)

That distinction matters in Python because enum members are objects, not just plain integers.

TypeScript: Object.values, with Care

For string enums, this is easy:

typescript
1enum Direction {
2  North = "NORTH",
3  South = "SOUTH",
4  East = "EAST",
5  West = "WEST"
6}
7
8const values = Object.values(Direction);
9console.log(values);

Numeric enums need more care because TypeScript creates reverse mappings, so Object.values() returns both names and numeric values.

typescript
1enum Status {
2  Pending,
3  Done
4}
5
6const values = Object.values(Status).filter(v => typeof v === "number") as number[];
7console.log(values);

That is one of the few cases where the language-specific behavior is slightly surprising.

Why the Order Usually Matters

Most languages return enum values in declaration order. That is useful for:

  • rendering dropdown menus
  • building validation lists
  • iterating through known states
  • generating documentation or mappings

If the UI needs a different order, sort explicitly rather than relying on a different declaration style to imply presentation order indirectly.

Arrays of Names Versus Arrays of Values

The phrase "all enum values" can mean different things:

  • the enum members themselves
  • their names as strings
  • their underlying numeric or string values

Examples:

Python names:

python
names = [item.name for item in Direction]

Java names:

java
String[] names = java.util.Arrays.stream(Direction.values())
                                 .map(Enum::name)
                                 .toArray(String[]::new);

So before choosing an API, decide what "value" means in your specific use case.

Use the Built-In API, Not Manual Lists

Avoid writing manual arrays like this unless you have a special reason:

java
Direction[] values = {Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST};

That duplicates information already present in the enum definition and can drift out of sync later.

The built-in enum enumeration API is safer because it always reflects the actual enum declaration.

Common Pitfalls

  • Forgetting that different languages return enum members, names, or underlying values in slightly different forms.
  • Manually maintaining a separate array instead of using the enum's built-in enumeration API.
  • Misusing Object.values() on numeric TypeScript enums without filtering reverse-mapped entries.
  • Assuming a specific ordering without confirming that the language uses declaration order.
  • Confusing enum member objects with their raw stored values in languages such as Python.

Summary

  • Most languages provide a built-in way to enumerate all enum members.
  • Java uses values(), C# uses Enum.GetValues, Python uses iteration, and TypeScript often uses Object.values().
  • The result may represent members, names, or underlying values depending on the language and the code you write afterward.
  • Use the built-in enumeration API instead of duplicating enum members in a separate array.
  • Be especially careful with TypeScript numeric enums because of reverse mappings.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms