Surprised the exact implementation is not touched. While what you have posted in the question is the simplest form, the complete implementation (including enumerator disposal, casting etc) is in the 8.8.4 section of the spec.
Now there are 2 scenarios where a foreach loop can be run on a type:
If the type has a public/non-static/non-generic/parameterless method named GetEnumerator which returns something that has a public MoveNext method and a public Current property. As noted by Mr Eric Lippert in this blog article, this was designed so as to accommodate pre generic era for both type safety and boxing related performance issues in case of value types. Note that this a case of duck typing. For instance this works:
class Test
{
public SomethingEnumerator GetEnumerator()
{
}
}
class SomethingEnumerator
{
public Something Current //could return anything
{
get { return ... }
}
public bool MoveNext()
{
}
}
//now you can call
foreach (Something thing in new Test()) //type safe
{
}
This is then translated by the compiler to:
E enumerator = (collection).GetEnumerator();
try {
ElementType element; //pre C# 5
while (enumerator.MoveNext()) {
ElementType element; //post C# 5
element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
If the type implements IEnumerable where theGetEnumerator returns IEnumerator that has a public MoveNext method and a public Current property. But an interesting sub case is that even if you implement IEnumerable explicitly (ie no public GetEnumerator method on Test class), you can have a foreach.
class Test : IEnumerable
{
IEnumerator IEnumerable.GetEnumerator()
{
}
}
This is because in this case foreach is implemented as (provided there is no other public GetEnumerator method in the class):
IEnumerator enumerator = ((IEnumerable)(collection)).GetEnumerator();
try {
ElementType element; //pre C# 5
while (enumerator.MoveNext()) {
ElementType element; //post C# 5
element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
If the type implements IEnumerable explicitly then the foreach is converted to (provided there is no other public GetEnumerator method in the class):
IEnumerator enumerator = ((IEnumerable)(collection)).GetEnumerator();
try {
ElementType element; //pre C# 5
while (enumerator.MoveNext()) {
ElementType element; //post C# 5
element = (ElementType)enumerator.Current; //Current is `T` which is cast
statement;
}
}
finally {
enumerator.Dispose(); //Enumerator implements IDisposable
}
Few interesting things to note are:
In both the above cases the Enumerator class should have a public MoveNext method and a public Current property. In other words, if you're implementing IEnumerator interface it has to be implemented implicitly. For eg, foreach wont work for this enumerator:
public class MyEnumerator : IEnumerator
{
void IEnumerator.Reset()
{
throw new NotImplementedException();
}
object IEnumerator.Current
{
get { throw new NotImplementedException(); }
}
bool IEnumerator.MoveNext()
{
throw new NotImplementedException();
}
}
(Thanks Roy Namir for pointing this out. foreach implementation isnt as easy it seems on the surface)
Enumerator precedence - It goes like if you have a public GetEnumerator method, then that is the default choice of foreach irrespective of who is implementing it. For example:
class Test : IEnumerable
{
public SomethingEnumerator GetEnumerator()
{
//this one is called
}
IEnumerator IEnumerable.GetEnumerator()
{
}
}
If you don't have a public implementation (ie only explicit implementation), then precedence goes like IEnumerator > IEnumerator.
There is a cast operator involved in the implementation of foreach where the collection element is cast back to the type (specified in the foreach loop itself). Which means even if you had written the SomethingEnumerator like this:
class SomethingEnumerator
{
public object Current //returns object this time
{
get { return ... }
}
public bool MoveNext()
{
}
}
You could write:
foreach (Something thing in new Test())
{
}
Because Something is type compatible with object, going by C# rules ,or in other words, the compiler lets it if there is an explicit cast possible between the two types. Otherwise the compiler prevents it. The actual cast is performed at run time which may or may not fail.