Did you know that... CSharp´s foreach
IEnumerator is a simple interface that defines a basic low level protocol to traverse a collection in a forward-only manner. The protocol is so simple:
Collections normally don’t implement enumerators directly. Instead, they provide enumerators via IEnumerable interface:
The benefit of implementing this interface in a collection is that several consumers of the collection can enumerate it without interfering with each other.
Enumerable can be thought of as IEnumeratorProvider.
So we could walk a collection like this:
However we have a great syntactic sugar for Enumerable: foreach.
The compiler will convert the former foreach into the previous enumeration using the underlying Enumerator.
There’s more…
We normally will make use of the generic counterparts, IEnumerator
So let’s go to the point of this post. Let’s see real magic in action.
- This is what we write:
- Compiler converts this to use an using block because of IDisposable nature of IEnumerable + Compiler converts it to use underlying enumerator:
- Compiler converts this using block to call Dispose defined in IDisposable:
(I know I have a compiling error but you get the idea right? :))
If we compare the first piece with the last one we can save a few lines of code, increasing developers productivity and expressing ideas in a more concise manner.
This is the magic that happens behind the scenes if we call a foreach using a disposable collection.
Keep coding!