​​Foreach Loop in C# (part 3)



As mentioned before, the foreach loop can be used to iterate any class that has implemented the IEnumerable interface. The following example demonstrates how to implement the IEnumerable interface in order to use the foreach loop with the custom class.



Above, the Shop class has implemented the IEnumerable interface that contains the GetEnumerator() method. This will enable the Shop class to be used with the foreach loop that returns the Customer objects.