I noticed that IEnumerable (Generics) requires 2 methods to be implemented:
1. IEnumerator<T> GetEnumerator()
2. IEnumerable.GetEnumerator() Method
(http://msdn.microsoft.com/en-us/library/19e6zeyy.aspx)
What is #2 and how I am supposed to define/implement this?
From my playing around with some code, it seems like the IEnumerator GetEnumerator() is called in the foreach loop.
Where does #2 IEnumerable.GetEnumerator come into play?
I was going through recipe 6.1 Creating an Iterator on a Generic Type in Oreilly’s C# 3.0 Cookbook and I was getting an error because #2 was not implemented (It wasn’t included in the recipe code).
Thank you!
The first is the generic GetEnumerator, while the second is the non-generic. They do functionally the same work, but the non-generic can only return an
objecttype.The easiest way to implement these is to implement the generic version, then provide this for the non-generic: