Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out all possible values of an enum? [duplicate]

Tags:

c#

enums

Possible Duplicate:
How do I enumerate an enum?

Say I have an enum type MyEnum. Is there a way in C# to get a list of all possible values for an enum of type MyEnum?

like image 621
Alex Baranosky Avatar asked Sep 02 '09 07:09

Alex Baranosky


People also ask

Can enum have duplicate values?

CA1069: Enums should not have duplicate values.

Can you forEach an enum?

Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.

Can two elements in enum have same value?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.


2 Answers

Enum.GetValues

like image 74
Oliver Hanappi Avatar answered Sep 30 '22 09:09

Oliver Hanappi


An instance of the enum can have any assignable to the underlying type (i.e., int.MinValue through int.MaxValue for any regular enum). You can get a list of the named values by calling Enum.GetNames and Enum.GetValues.

like image 26
Sam Harwell Avatar answered Sep 30 '22 11:09

Sam Harwell