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.
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.
This returns an array in declaration order.
C#: Use Enum.GetValues
In C#, use Enum.GetValues and cast the result to the enum type.
In newer C# versions, a generic form is available too:
That is the cleanest version when supported.
Python: Convert the Enum to a List
Python's Enum type is iterable.
If you want only the raw member values rather than enum objects:
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:
Numeric enums need more care because TypeScript creates reverse mappings, so Object.values() returns both names and numeric 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:
Java names:
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:
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# usesEnum.GetValues, Python uses iteration, and TypeScript often usesObject.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.
Related reading
- How to get all hugging face models list using python?
- How to get all possible 2N combinations of a list’s elements, of any length
- How to get all possible combinations from two arrays?
- How to get all subsets of a set? powerset
- How to get different colored lines for different plots in a single figure
- How to get distinct values from an array of objects in JavaScript?
- How to get first element in a list of tuples?
- How to get first N number of elements from an array

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 courseData 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.