Iterator: Enumerator
If you are creating a class that behaves and behaves like a collection, it is convenient to allow users of the class to enumerate the members of the collection using a foreach statement. This is easier to implement in C # 2.0 than C # 1.1. As a demo, we first add enumerations to a simple collection in C # 1.1, and then we modify this example to use the new c#2.0 enumeration to build the method.
We'll start with creating a simplistic list box, which will contain an array of 8 strings and an integer that records how many strings have been added to the array. Constructors initialize an array and populate it with parameters passed in.
public ListBox(params string[] initialStrings)
{
strings = new String[8];
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
In addition, the ListBox class requires an Add method (an action to add a string) and a method that returns the number of strings in the array.
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
public int GetNumEntries()
{
return ctr;
}
Note: In actual development, ArrayList is usually used instead of fixed-size arrays. In order to simplify the program there is no detection of the bounds of the array subscript.
In the sense that the listbox is a collection, it would be convenient to get all the strings in the listbox using a Foreach loop that is typically used in the collection. In this case, you can write code like this:
ListBox lb = new ListBox("a", "b", "c", "d", "e", "f", "g", "h");
foreach (string s in lb) {
Console.WriteLine(s);
}
However, you will get a mistake like this:
"Iterator.listbox" does not contain a public definition of "GetEnumerator", so a foreach statement cannot act on a variable of type "Iterator.listbox"
To use the foreach statement, you must also implement the IEnumerable interface.
This interface only requires implementation of one method: GetEnumerator. This method must return an object that implements the IEnumerator interface. In addition, we need to return this object not only to implement the IEnumerator, but also to know how to enumerate the ListBox object. You will need to create a listboxemunerator (described below):
Note:ienumerable and IEnumerator are different interfaces, please don't confuse them.
public IEnumerator GetEnumerator()
{
return new ListBoxEnumerator();
}
The ListBox can now use a Foreach loop:
ListBox lbt = new ListBox("Hello", "World");
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");
foreach (string s in lbt)
{
Console.WriteLine("Value: {0}", s);
}
The listbox is instantiated first, and two strings are initialized, followed by the addition of four. The Foreach Loop accepts the listbox instance and iterates it, returning the string in turn. The output is:
Hello
World
Who
Is
John
Galt
Implement IEnumerator interface
Note that listboxenumerator not only needs to implement the IEnumerator interface, it also needs some special understanding for the ListBox class; In particular, it must be able to obtain a string array of the ListBox and traverse its contained strings. The relationship between the IEnumerable class and its associated IEnumerator class is a bit tricky. The best way to implement the IEnumerator interface is to create a nested IEnumerator class in the IEnumerable class.
public class ListBox : IEnumerable
{
// 嵌套的私有ListBoxEnumerator类实现
private class ListBoxEnumerator : IEnumerator
{
// 代码实现...
}
// ListBox类的代码...
}
Note that Listboxenumerator needs a reference to the ListBox class it embeds. You can pass the Listboxenumerator constructor.