How do I convert an enum to a list in C?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Enumerations, or enums, are a special "class" representing a group of constants (unchangeable/read-only variables). Enums are used to create named constants for underlying integral types such as byte, int, etc. Often you might need to convert these enum values into a list for easier processing, iteration, or for use in a UI component. This article provides insights into converting enums to a list in C#.
Understanding Enumerations in C#
Before diving into converting enums to lists, let's brush up on what an enumeration is in C#.
Basic Enum Definition
The enum Colors has four named constants (Red, Green, Blue, Yellow). Internally, these are represented as integer values starting at 0 by default, unless explicitly assigned.
Enum to List Conversion
If you want to iterate over the values of an enum or use them as a collection, converting them to a list is a straightforward solution.
Using LINQ to Convert Enums to List
The most common and clean way to convert an enum to a list is by utilizing LINQ (Language Integrated Query):
Explanation
- Enum.GetValues: Retrieves an array of the values in the enum.
- Cast<T>(): Converts the array to an
IEnumerable<T>, specifically toIEnumerable<Colors>in our case. - ToList(): Converts the
IEnumerable<Colors>into aList<Colors>.
Alternative Methods
Besides LINQ, there are other methods to perform this conversion:
Using a Loop
Although LINQ simplifies the process significantly, you can manually loop through the enum values:
Using Array and ToList
You can first convert the enum to an array and then to a list:
Key Considerations
- Performance: Using LINQ is generally more concise but might not always be the most performant option for large enums due to additional method calls.
- Versioning: Enums in C# are static lists. Once defined and compiled, adding or removing values requires recompilation.
- Usages:
- Converting enums to lists can be particularly useful in user interfaces where you need to display options in combo boxes, drop-down lists, etc.
- Creating a list allows easier manipulation, such as filtering or sorting.
Summary Table
Here's a concise summary of converting enums to lists:
| Method | Description | Advantages | Disadvantages |
| LINQ Casting | Uses Enum.GetValues, Cast<T>(), ToList()
to convert enum to list. | Concise and idiomatic C# code | Slightly increased overhead for large enums |
| Looping | Iterates through Enum.GetValues adding
each to a list manually. | Straightforward for small enums | Verbose, potentially inefficient for large enums |
| Array Conversion | Converts first to an array and then to a list. | Direct and efficient | Two-step conversion, more manual work |
Additional Considerations
- Flags Attribute: Enums can be attributed with
[Flags]to be treated as bit fields. Handling these may require additional logic if converting to a list. - Serialization: Converting enums into lists could help in scenarios such as serializing to JSON or XML where enums need to be sent over the wire.
By converting enums to lists, you can effectively use them in contexts that require collections, thereby improving the versatility and functionality of your C# applications.
Related reading
- How do I convert Long to byte and back in java
- How do I convert struct System.Byte byte to a System.IO.Stream object in C?
- How do I convert this list of dictionaries to a csv file?
- How do I count occurrence of unique values inside a list
- How do I convert an Excel serial date number to a .NET DateTime?
- How do I convert an Int to a String in C without using ToString?
- How do I count the occurrences of a list item?
- How do I create a gain chart in R for a decision tree model?

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.