C# - how to iterate through an enum

Frequently I find I wish to iterate through an enum type to add items to a list or do something with each item. Doing so is simple, but remembering how causes a good few minutes of Google searching, so this post is here as an aid to my memory the next time I wish to iterate over an enum.

To iterate over an enum, we simply need to get hold of the values the enum contains, which is done by using the method 'GetValues' associated with the Enum class, like so.

That's all there is to it. Like I said, a nice, short, simple post to aid iterating through C# enums in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *