IEnumerable is the base interface for all collections that can be enumerated.
IEnumerable contains a single method, GetEnumerator, which returns an
IEnumerator. IEnumerator provides the ability to iterate through the
collection by exposing a Current property and MoveNext and Reset
methods.
It is a best practice to implement IEnumerable and IEnumerator on your collection classes to enable the foreach statement.
Example program for IEnumerable in C#.NET
using System;
using System.Collections;
namespace ProgramCall
{
class person : IEnumerable
{
private int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public int Age { get; set; }
public string Name { get; set; }
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
}
class MainClass
{
static void Main(string[] args)
{
person personobj = new person();
foreach(var n in personobj)
{
Console.WriteLine(n);
}
Console.Read();
}
}
}