SIGN UP MEMBER LOGIN:    
ARTICLE

Iterator in C# 2.0

Posted by Munir Shaikh Articles | Visual C# August 21, 2007
Iterator is one of the new feature in C# 2.0. I am trying to explain it with Employees and Department classses.
Reader Level:

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);

            }

        }

    }

} 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor