Wednesday, March 08, 2006
Reset? Reset!
Herb Schildt's book "C# 2.0 The Complete Reference" claims that C# enumerators have a Reset() method that restores the inital state created by a GetEnumerator() method on an enumerable object (pp. 737 and 765).
Great idea, Herb! Do tell Microsoft about it, too, won't you? When you try this method for enumerators over generic collections, you will be surprised. It's not there.
Most enumerators, like those for arrays, have a Reset() method. However, for generic collections Microsoft chose not to implement the usual IEnumerator interface, instead implementing the "IEnumerator Generic" interface, which lacks a Reset() method. ASP.NET 2.0 documentation is otherwise opaque. The Reset() method does not depend on data types of a collection and could easily have been implemented.
The workaround is to execute the required GetEnumerator method again. That will require making the source object accessible when otherwise it would not have been needed.
Great idea, Herb! Do tell Microsoft about it, too, won't you? When you try this method for enumerators over generic collections, you will be surprised. It's not there.
Most enumerators, like those for arrays, have a Reset() method. However, for generic collections Microsoft chose not to implement the usual IEnumerator interface, instead implementing the "IEnumerator Generic" interface, which lacks a Reset() method. ASP.NET 2.0 documentation is otherwise opaque. The Reset() method does not depend on data types of a collection and could easily have been implemented.
The workaround is to execute the required GetEnumerator method again. That will require making the source object accessible when otherwise it would not have been needed.
Subscribe to Posts [Atom]