IEnumerable.GetEnumerator() returns an enumerator object (which implements IEnumerator). This has a property "Current" which returns the current item, and a method "MoveNext()" which advances the enumerator on to the next item.
This is all the foreach loop needs to perform the foreach. It simply assigns the "Current" property to a variable then executes .MoveNext() each time.
Usually, when you implement IEnumerable on a class, you know where the source is coming from. So if you had a class such as this
public class MyEnumerable
{
private List<string> items;
}
The implementation of IEnumerable would look like this
public class MyEnumerable : IEnumerable<string>
{
private List<string> items;
public IEnumerator<string> GetEnumerator()
{
return items.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
}
Notice that the enumerator is coming from the list object so you don't have to worry about creating an enumerator (though you can if you need to by implementing IEnumerator)