How to Create Iterator to Process
Generic List in C# (C Sharp)

Hope you are familiar with iterators in C#. You might have used iterators on lists or any collection whose type is defined. In addition to it, you can also use iterators to iterate and process a generic list. This article will guide you on creating a generic list and processing it using iterators.


To start with, here is a sample code using iterators to process an arraylist of strings:

public class sampleClass {
public static void Main( ) {
ArrayList stringArray = new ArrayList( );
stringArray.Add(“Hai”);
stringArray.Add(“Hello”);
stringArray.Add(“Nice”);
processList(stringArray);
}
public void processList(ArrayList stringList) {
foreach (object element in stringList) {
Console.WriteLine(element);
}
}
}

At the time of compilation, the content of processList (highlighted in bold) will be translated to:

Enumerator eNum1 = stringList.GetEnumerator();
while(eNum1.MoveNext()) {
object element = eNum1.Current;
Console.WriteLine(element);
}

To your surprise, the method processList is using enumerators to iterate over the arraylist. Same happens with generic list as well, but only difference is that you have to explicitly use enumerators in your code. You cannot wait for the translation to happen automatically. But why do you use generic list? The above example is restricted to an arraylist containing strings. You can make the code generic to accept elements of any type and not just strings. With this introduction on generic types, here is an example to show how iterators can be used to process a generic list:

public class sampleGeneric<T> : IEnumerable<T> {
private T[ ] genericList = new T[10];
private int index = 0;
public void incrementList(T element) {
index++;
genericList[index] = element;
}
public T decrementList() {
index- -;
return genericList[index];
}
public IEnumerator<T> GetEnum() {
for (int element = 0; element < index; element++ ) {
yield return genericList[element];
}
}
IEnumerator GetEnumerator() {
return GetEnum();
}

public IEnumerable<T> iterateList {
get {
return this;
}
}
}
class testClass {
static void Main() {
sampleGeneric <int> list = new sampleGeneric <int>();
for (int index = 100; index < 110; index++) {
list.incrementList(index);
}
foreach (int element in list) {
System.Console.Write("{0} ", element);
}
System.Console.WriteLine();

foreach (int n in list.iterateList) {
System.Console.Write("{0} ", element);
}
}
}

Output of this code will be:
100 101 102 103 104 105 106 107 108 109
100 101 102 103 104 105 106 107 108 109

An analysis of this example is done in the rest of the article.

Class Name: sampleGeneric – a generic class

Private Members:

• genericList – A list containing 10 elements wherein the type of the list is determined only at run time. This type is going to be same as the type associated with the sampleGeneric class
• index – An integer variable used to indicate the current position in the list

Methods:

• incrementList – Increments the index member and assigns the value passed to the function as the list element pointed by the index
• decrementList – Decrements the index thereby detaches the last element (referred by the index) from the list
• GetEnum() – Define a new method that returns each element of the genericList. Note that yield keyword is used along with return statement. This is done to return only one element at a time when called from an iterator and display the next element when the next iteration is triggered
• GetEnumerator - Explicitly override the GetEnumerator() method of IEnumerable to ensure that the sampleGeneric class can be accessible using an iterator or a foreach statement. In this method, you have made a call to the GetEnum() method discussed above

Public Members:

• iterateList - This member of sampleGeneric holds the object referring to genericList. The object is assigned to It using the get accessor method. Since you are not going to alter the object using this member, set accessor method is not used.

Class Name: testClass – normal class used to test the sampleGeneric class

Methods:

• Main – In this Main method, sampleGeneric class is instantiated and populated with a list of integers. The list is then accessed using foreach statement thus using iterators. In addition, you also display the list by calling iterateList member of sampleGeneric class. This triggers the get accessor method of iterateList and the object is returned. Since the foreach statement displays the elements in the default order as it is recorded in iterateList, both of them give the same output.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How does Yield Keyword of C# Use Lazy Evaluationm: Reference Types |How to Create Iterator to Process Generic List in C# | How to Use “add” and “remove” Contextual Keywords of C# | How to Use ForEach with Arrays in C# |Illustration of Operator Keywords (as, is) with Examples in C# | Illustration of Operator Keywords (new, sizeof) with Examples in C# |Illustration of Operator Keyword stackalloc with Examples in C# |Illustration of Operator Keywords (true, false) with Examples in C#|Illustration of Operator Keyword typeof with Examples in C# |List of Contextual Keywords in C#|


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.