Lot’s of people asked me why IEnumerator<T> extends IDisposable. We did this to support some obscure, yet important scenarios where an enumerator enumerates database rows, or files in a directory, etc. In such cases, the enumerator usually opens some connection or a handle, which then needs to be closed at the end of the enumeration.
Now, we could solve the problem in two ways.
1) We could do what we did. Now, foreach loop can simply call Dispose when the loop terminates.
2) We could implement foreach loop to do a dynamic cast to IDisposable at the end of the loop and if the enumerator happened to implement IDisposable, we would call it.
We chose #1 because it’s slightly faster and the cost of implementing an empty Dispose method, for those who don’t need it, is relatively low.
… besides, have you heard about the new C# yield statement?