Alex Rivera | Logout

How to get an array of all enum values in C#?

Asked 2010-09-28T20:21:37.953
122

I have an enum that I'd like to display all possible values of. Is there a way to get an array or list of all the possible values of the enum instead of manually creating such a list? e.g. If I have an enum:

public enum Enumnum { TypeA, TypeB, TypeC, TypeD }

how would I be able to get a List<Enumnum> that contains { TypeA, TypeB, TypeC, TypeD }?

Edit
Report

2 Answers

20

Try this code:

Enum.GetNames(typeof(Enumnum));

This return a string[] with all the enum names of the chosen enum.

answered 2010-09-28T20:23:57.523
8
Enum.GetValues(typeof(Enumnum));

returns an array of the values in the Enum.

answered 2010-09-28T20:23:50.033

Your Answer