xxxxxxxxxx
/*
IEnumrable interface:
has a single method, "GetEnumerator" a single method that allows you to iterate over your class object using foreach.
You must implement this function.
*/
// For example, we will implement the IEnumerable interface in our home-made collection.
public class MyCollection:IEnumerable
{
private readonly int[] _list;
public MyCollection(int size)
{
_list = new int[size];
}
public IEnumerator GetEnumerator()
{
foreach (var item in _list)
{
yield return item;
}
}
}
public class Program
{
static void Main(string[] args)
{
MyCollection list = new MyCollection(5);
foreach (var variable in list)
Console.WriteLine(variable); // 0 0 0 0 0
}
}