Please forgive me if my Answer is a bit off-topic and many a bit too advanced to you. I still think it could be very useful.
I want to reference my CodeProject article which shows some good and simple enumeration techniques many developers do not know. But more intriguing is the main topic. I may come as surprise, but C# (as well as C/C++) enumeration types are not "real" enumeration types as in some other languages!
The reason is very convincing: they do not enumerate! In .NET terms it means they do not implement the interface
IEnumerable
needed to enumerate using
foraech
. Note that some other languages do it inherently.
The article overview the ways to work around, but the main result is the enumerator build around any existing type. Also, it protects from some common fallacies related to main topic.
Please see:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^].
Good luck,
—SA