Foreach On IEnumerable



Let me make few statements as my background for concept.

The IEnumerable<T> interface is a generic interface that provides an abstraction for looping over elements. In addition to providing foreach support, it allows you to use extension methods in the System.Linq namespace.  

We can only use foreach loop on IEnumerable variable.

IEnumerable is interface enforce rule that we need to implement GetEnumerator method.

GetEnumerator method return IEnumerator following is the signature for the GetEnumerator method.

public IEnumerator GetEnumerator(void) 

Now we have to implement IEnumerator interface with IEnumerable Interface in order to accept return value form the GetEnumerator method.

IEnumerator interface enforce rules that we need to implement as follow:s

  • MoveNext method [public bool MoveNext(void)]
    • Increment collection counter by one.
    • If end of collection has been reached it will return false else return true.
  • Reset method [pubic void Reset(void)]
    • Resets the collection index to its initial value of -1.
  • Current method [public object Current(void)]
    • Return the current index object from collection.

Using the code

public class Employee
    private int employee_Id;
    private string employee_Name;

    public Employee(int employee_Id, string employee_Name)
        this.employee_Id = employee_Id;
        this.employee_Name = employee_Name;

    public int Employee_Id { get { 
                                    return employee_Id; 
                             set {

    public string Employee_Name {
                                    get {
                                          return employee_Name; 
                                    set { 

Employee class is single unit type in foreach loop.

We have exposed two properties Employee_Id and Employee_Name.

Now let’s start creating our collection that would be applied on Foreach loop.

Let’s start following statements that we had state in the background.

Create class Employees and implement IEnumerable and IEnumerator interface.

We will also create Employee class array collection and initialize in constructor by adding Employee object into collection of Employee array.

public class Employees : IEnumerable,IEnumerator
    public Employee[] employeeList;
    int position = -1;

    public Employees()
        employeeList = new Employee[4]{
                    new Employee(1,"Amey"),
                    new Employee(2,"Pushkar"),
                    new Employee(3,"Raju"),
                    new Employee(5,"Vijay")

    public object Current
                return employeeList[position];
            catch (IndexOutOfRangeException)
                throw new InvalidOperationException();

    public bool MoveNext()
        return (position < employeeList.Length);

    public void Reset()
        position = 0;

    public IEnumerator GetEnumerator()
        return (IEnumerator)this;

Let’s move to our Program class contain the Main method.

Let’s use foreach loop on Employees class  which contain the array collection of Employee.

Add the following code in the Main method.

static void Main(string[] args)
    var employees = new Employees();
    foreach (Employee employee in employees)
       Console.WriteLine("Employee Id:- "+employee.Employee_Id.ToString()
                          +"Employee Name:- "+employee.Employee_Name);



Run the project.

You will observe the following output.

Employee Id: - 1     Employee Name: - Amey
Employee Id: - 2     Employee Name: - Pushkar
Employee Id: - 3     Employee Name: - Raju 
Employee Id: - 5     Employee Name: - Vijay
By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s