Iterator in C# 2.0


You can iterate over data structures such as arrays and collections using a foreach loop:

 

string[] cities = {"New York","Paris","London"};

foreach(string city in cities)

{

    Console.WriteLine(city);

}

 

In fact, you can use any custom data collection in the foreach loop, as long as that collection type implements a GetEnumerator method that returns an IEnumerator interface. Usually you do this by implementing the IEnumerable interface:

 

public interface IEnumerable

{

    IEnumerator GetEnumerator();

}

public interface IEnumerator

{

    object Current { get;}

    bool MoveNext();

    void Reset();

}

 

Iterator is one of the new feature in c# 2.0 which provide a way to create classes that can be used with foreach statement without implementing the IEnumerator & IEnumerable interfaces when compiler detects iterator it will automatically generate the current, MoveNext and dispose method of IEnumerable or IEnumerator interface. Here I explain with employees and department classes GetEnumerator method, typically by implementing IEnumerable or IEnumerable <ItemType>. You tell the compiler what to yield using the new C# yield return statement.

 

Employees:

 

using System;

using System.Collections.Generic;

using System.Text;

namespace CsharpIterators

{

    class Employees

    {

        private string _name;

        public string EmployeeName

        {

            get { return _name; }

            set { _name = value; }

        }

        public Employees(string name)

        {

            _name = name;

        }

        public Employees()

        {

        }

    }

}

Department:

using System;

using System.Collections.Generic;

using System.Text;

namespace CsharpIterators

{

    class Department

    {

        private List<Employees> _employees;

        private string _name;

        public string DepartmentName

        {

            get { return _name; }

            set { _name = value; }

        }

        public Department(string name)

            : this()

        {

            _name = name;

        }

        public Department()

        {

            _employees = new List<Employees>(5);

        }

        public void AddEmployees(Employees emp)

        {

            _employees.Add(emp);

        }

        public IEnumerator<Employees> GetEnumerator()

        {

            foreach (Employees emp in _employees)

            yield return emp;

        }

    }

}

Program:

using System;

using System.Collections.Generic;

using System.Text;

namespace CsharpIterators

{

    class Program

    {

        static void Main(string[] args)

        {

            Employees emp1 = new Employees("Jack");

            Employees emp2 = new Employees("Radu");

            Employees emp3 = new Employees("Emali");

            Employees emp4 = new Employees("Jecci");

            Department dept = new Department("MyDepartment");

            dept.AddEmployees(emp1);

            dept.AddEmployees(emp2);

            dept.AddEmployees(emp3);

            dept.AddEmployees(emp4);

            foreach (Employees emp in dept)

            {

                Console.WriteLine(emp.EmployeeName);

            }

        }

    }

} 

Up Next
    Ebook Download
    View all
    Learn
    View all