CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Iterators in C#

By Satheesh babu
Posted On Feb 18,2008
Article Rating:
Be first to rate
this article.
No of Comments: 5
Category:
Print this article.

Iterators in C#

If we are implementing collections for our business object (BO) or entity, it is obvious that we need to iterate through the collection to access the members. This article will help us to understand the implementation of a custom enumerator with C# 2.0 for iterating the collection. To understand the implementation of custom collection please read my article on Custom Collection in C# - Part 2 and Custom Collection in C# - Part 1 . First section of this article deals with creation of custom enumerator in a traditional way while in the next section we will learn to implement the same with the power of C# 2.0 yield keyword.

 

Implementing Iterator:

To make the subject matter easily understandable I will take the following Employee BO,

 

public class Employee

{

private int _Age;

private string _Name;

 

public int Age

{

get { return _Age; }

set { _Age = value; }

}

 

public string Name

{

get { return _Name; }

set { _Name = value; }

}

 

}

 

Say, we have an object called EmployeeCollection that has a list of Employee object.

 

public class EmployeeCollection

{

private List<Employee> _list = new List<Employee>();

 

public List<Employee> Employees

{

set

{

_list = value;

}

}

}

 




Suppose from the above employee collection if I need to iterate into all the employees whose age is greater than 50 using for-each statement and display their name. To achieve this I need to implement a custom enumerator that gives me the employees with age greater than 50.
In the coming sections, I will take you through the traditional implementation that will implement IEnumerable, IEnumerator interfaces called SeniorEmployeeEnumerator, means I need to implement MoveNext(),Reset() and Current of IEnumerator and GetEnumerator() of IEnumerable interface in SeniorEmployeeEnumerator class explicitly to iterate the collection.

 

The syntax of IEnumerator<T> is,

 

public interface IEnumerator<T> : IDisposable, IEnumerator

 

The syntax of IEnumerable<T> interface is,

 

public interface IEnumerable<T> : IEnumerable

 

Here is the implementation of SeniorEmployeeEnumerator object.

 

public class SeniorEmployeeEnumerator : IEnumerable<Employee>,

IEnumerator<Employee>

{

List<Employee> list = new List<Employee>();

private Int32 _inUseIndex = 0;

private Int32 _moveNextStartIndex = 0;

 

private int MoveNextStartIndex

{

get { return _moveNextStartIndex; }

set { _moveNextStartIndex = value; }

}

 

private int InUseIndex

{

get { return _inUseIndex; }

set { _inUseIndex = value; }

}

 

#region IEnumerator Members

public bool MoveNext()

{

bool returnFlag = false;

for (Int32 index = MoveNextStartIndex; index <= list.Count - 1; index++)

{

if (list[index].Age > 50)

{

InUseIndex = index;

MoveNextStartIndex = index + 1;

returnFlag = true;

break;

}

}

return returnFlag;

}

public void Reset()

{

_inUseIndex = 0;

_moveNextStartIndex = 0;

}

 

public Employee Current

{

get

{

if ((InUseIndex < 0) | (InUseIndex >= list.Count))

{

throw new InvalidOperationException("The enumerator is positioned outside the bounds of the collection elements.");

}

return list[InUseIndex];

 

}

}

object IEnumerator.Current

{

get

{

return Current;

}

}

 

#endregion

 

void IDisposable.Dispose()

{

 

}

public SeniorEmployeeEnumerator(List<Employee> emplist)

{

list = emplist;

}

#region IEnumerable Members

public IEnumerator<Employee> GetEnumerator()

{

return this;

}

IEnumerator IEnumerable.GetEnumerator()

{

return (GetEnumerator());

}

#endregion

}

 

To get the enumerator that iterates through the employees collection with age greater than 50 we need to add a function in EmployeeCollection that return back the enumerartor,

 

public SeniorEmployeeEnumerator GetSeniorEmployeeEnumerator()

{

SeniorEmployeeEnumerator enume = new SeniorEmployeeEnumerator(_list);

return enume;

}

 

So totally we have 3 objects,

 

Employee – BO or Entity

EmployeeCollection – Collection of Employee BO

SeniorEmployeeEnumerator - Custom enumerator for iterating the collection.

 

Usage:

List<Employee> emplist = new List<Employee>();

Employee emp1 = new Employee();

emp1.Name = "Fatima";

emp1.Age = 57;

emplist.Add(emp1);

 

Employee emp2 = new Employee();

emp2.Name = "Evangeline";

emp2.Age = 52;

emplist.Add(emp2);

 

Employee emp3 = new Employee();

emp3.Name = "Damien";

emp3.Age =49;

emplist.Add(emp3);

 

Employee emp4 = new Employee();

emp4.Name = "Cameroon";

emp4.Age = 55;

emplist.Add(emp4);

 

Employee emp5 = new Employee();

emp5.Name = "Babu";

emp5.Age = 24;

emplist.Add(emp5);

 

EmployeeCollection empcoll = new EmployeeCollection();

empcoll.Employees = emplist;

foreach (Employee emp in empcoll.GetSeniorEmployeeEnumerator())

{

Response.Write(emp.Name + " " + emp.Age + "");

}

 

The output will be,

 

OUTPUT:

Fatima 57
Evangeline 52
Cameroon 55

 

yield keyword:

 

The above custom enumerator can be implemented easily with the use of yield keyword. yield keyword is used in GetEnumerator() method of IEnumerable interface and we don’t need to explicitly implement the MoveNext(),Reset() and Current of IEnumerator interface.

 

So our SeniorEmployeeEnumerator class will look like,

 

public class SeniorEmployeeEnumerator : IEnumerable<Employee>

{

List<Employee> list = new List<Employee>();

public SeniorEmployeeEnumerator(List<Employee> emplist)

{

list = emplist;

}

 

public IEnumerator<Employee> GetEnumerator()

{

for (int i = 0; i < list.Count; i++)

{

if (list[i].Age > 50)

{

yield return list[i];

}

}

}

IEnumerator IEnumerable.GetEnumerator()

{

return (GetEnumerator());

}

}

Again the output will be,

 

OUTPUT:

Fatima 57
Evangeline 52
Cameroon 55

 

Thus the yield keyword reduced the overhead of implementing IEnumerator<Employee>

Interface. Download the Example with this article. I have implemented one more enumerator that iterates into employees with age less than 50 called JuniorEmployeeEnumerator.

 

Download Source:

Download Source

 

Reference:

http://devlicio.us/blogs/derik_whittaker/archive/2006/10/05/Creating-and-Using-Custom-Collection-Enumerators.aspx

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
In the cotimpcaled w
In the cotimpcaled world we live in, it's good to find simple solutions. http://tvoqnbrld.com [url=http://kessud.com]kessud[/url] [link=http://qoqwpijbbf.com]qoqwpijbbf[/link]
It's great to read s
It's great to read something that's both enjoyable and provides <a href="http://elazctn.com">prsmiatgadc</a> solutions.
Dag nabbit good stuf
Dag nabbit good stuff you whraseipnpppers! http://dwaiqdxi.com [url=http://sfcdzdfw.com]sfcdzdfw[/url] [link=http://irfwfcoovh.com]irfwfcoovh[/link]
That kind of <a href
That kind of <a href="http://ybucngvhy.com">thnkiing</a> shows you're an expert
My hat is off to you
My hat is off to your astute command over this to-vrpbcaio!