From my technical blog:
Often you want to be able to enumerate through a collection of objects using the foreach statement in C#:
To be able to pull that off, myClass must implement IEnumerable:
As is says above, IEnumerable just exposes another interface, called IEnumerator:
To see this in action, let's create an example by which we can iterate through a list of students in a class using the foreach statement in C#.
First, we need to create a Student Class as shown below. At the minimum we want to override ToString() (Overriding System.Object.ToString() and IFormattable) so it says something meaningful and for kicks I decided to override Equals() and GetHashCode() (Object Identity vs. Object Equality - Overriding System.Object.Equals(Object obj)) only as an example.
Read more...