HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How do I convert an enum to a list in C#?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
listenumconverthow

Problem

Is there a way to convert an enum to a list that contains all the enum's options?

Solution

This will return an IEnumerable of all the values of an Enum.

Enum.GetValues(typeof(SomeEnum)).Cast();


If you want that to be a List, just add .ToList() after .Cast().

To use the Cast function on an Array you need to have the System.Linq in your using section.

Code Snippets

Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>();

Context

Stack Overflow Q#1167361, score: 1371

Revisions (0)

No revisions yet.