C# Iterators
Subject:   Isn't the C# 2.0 example cheating?
Date:   2005-01-28 10:53:07
From:   sjmac
The implementation of the c# v2.0 enumerator uses a foreach loop, which relies on the internal string[] already implementing IEnumerable and IEnumerator?

If you also used that fact for the C# 1.1 example, then you could omit all the code to implement the IEnumerator, and just replace the GetEnumerator call with
return strings.GetEnumerator();

Regarding the type saftey aspects, you also didn't mention that C# 1.1 doesn't mind if you miss off the IEnumerable and IEnumerator refernces in your class declaration. As long as you've implemented the GetEnumerator(), Current, MoveNext(), and Reset() methods then foreach will continue to work, and your "Enumerator" will be type safe. That seems like a bit of a hack in C# though, so maybe that's why you kept quiet ;-)

(See the Collection Classes Tutorial in the MSDN C# Programmer's Reference)