Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating on enum type [duplicate]

Tags:

Possible Duplicate:
How do I enumerate an enum?

I don't know if it is possible to do what I want to do, but I think why not.

I have example enum:

public enum Animals {     Cat,     Dog,     ... } 

What I want to do is to iterate on this enum. I want it work like that:

foreach( var type in Animals ) {     Console.WriteLine( type.toString() ); } 

and the output will be:

 Cat  Dog 

Is it possible? I don't want to put every item to an array, and then iterate, I want to iterate directly on this enum.

like image 955
nirmus Avatar asked Aug 10 '11 12:08

nirmus


People also ask

Can enum have duplicate values?

CA1069: Enums should not have duplicate values.

Can you loop through an enum?

Using for loopYou can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.

Can you loop through enum typescript?

Of course, it's also possible to loop over the keys of a string enum, using Object. keys().

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.


2 Answers

EDIT: Note that throughout this answer I've renamed Animals to Animal. According to .NET conventions, only flags-based enums should have a plural name.

You can use Enum.GetValues():

foreach (var type in (Animal[]) Enum.GetValues(typeof(Animal)) {     Console.WriteLine(type.toString()); } 

As noted in Dan's comment, if you use explicit typing in your foreach loop, you don't need to cast:

foreach (Animal type in Enum.GetValues(typeof(Animal)) {     Console.WriteLine(type.toString()); } 

But now you won't spot errors as early as you could. For example:

foreach (Animal type in Enum.GetValues(typeof(SomeEmptyEnum)) {     Console.WriteLine(type.toString()); } 

Where SomeEmptyEnum is (obviously) an empty enum type. Here GetValues will return an empty array of type SomeEmptyEnum[]. The code above will only check that each member of the returned array is of the right type, so you won't spot the problem. Obviously this is unlikely to happen in real life, but it demonstrates the sort of code smell which leads me to cast the result instead - basically I far prefer dealing with strongly-typed collections.


Alternatively, for a somewhat more typesafe approach, you can use my Unconstrained Melody library:

foreach (var type in Enums.GetValues<Animal>()) {     Console.WriteLine(type.ToString()); } 

Here my Enums.GetValues<T>() returns an IList<T>, which means:

  • There's no need to cast
  • It actually returns an immutable list, so it doesn't need to create a new collection each time, unlike the standard Enum.GetValues()

It's also got a generic constraint forcing T to be an enum type, so you can't accidentally call it with a non-enum type, unlike Enum.GetValues()...

like image 154
Jon Skeet Avatar answered Oct 29 '22 16:10

Jon Skeet


Enum.GetValues is your friend.

like image 36
Vijay Gill Avatar answered Oct 29 '22 14:10

Vijay Gill