foreach is just a shortcut, that simplifies performing the collection enumeration. From
http://support.microsoft.com/kb/307484[
^], which describes how to create a custom collection (implementing IEnumerable):
How does this work? For Each calls the GetEnumerator method to create the Enumerator object and calls the MoveNext method to set the cursor to the first item. Then the Current property is accessed to get the item in MyObj. This is repeated until MoveNext returns False.
In other words, your collection implements the IEnumerable interface, which requires a GetEnumerator method. This GetEnumerator method returns an IEnumerator that knows how to do the actual enumeration, traversing your collection.