An Enumerable Collection |
|
Introduction |
The IEnumerator interface is used to set up a collection for enumeration. The IEnumerator doesn't provide the functionality necessary to use foreach. The next step is to implement another interface called IEnumerable. While the IEnumerator interface is used to identify the class that holds each value that will be visited, the IEnumerable interface is used to communicate with the collection whose items will be enumerated. For this reason, when implementing this class, you should provide the means of accessing the external collection. This can be done by passing a collection of the class that holds the values, to a constructor of the IEnumerable implementer. |
To implement the IEnumerable interface, start by deriving a class from it. While the class implemented by the IEnumerator interface represents an object, the class that implements the IEnumerable is a collection. Here is an example: public class Enumerable : IEnumerable { } Notice that the new class doesn't know what collection it will be asked to enumerate. For this reason, in the new class, you should declare a member variable of the class that holds the values that will be enumerated. If the collection is array-based, you can create the field as follows: public class Enumerable : IEnumerable { private double[] numbers; } Eventually, when instantiating the IEnumerable implementer, you will need to pass it a collection of values. To make this possible, you can create a method in the new class and pass that collection of objects. Here is an example: public class Enumerable : IEnumerable { private double[] numbers; public void Identify(double[] values) { } } In this method, you can assign the member variable to the argument. You should also assign each member of the argument to its equivalent of the member of the argument. This can be done as follows: public class Enumerable : IEnumerable { private double[] numbers; public void Identify(double[] values) { numbers = values; for (int i = 0; i < values.Length; i++) numbers[i] = values[i]; } } To support the use of the foreach loop, the IEnumerable interface is equipped with (only) a method named GetEnumerator that you must implement. The IEnumerable.GetEnumerator() method returns an IEnumerator object. When implementing this method, you can return an object of the class that implements the IEnumerator interface, passing it the collection that was declared in the IEnumerable implementer. This can be done as follows: public class Enumerable : IEnumerable { private double[] numbers; public void Identify(double[] values) { numbers = values; for (int i = 0; i < values.Length; i++) numbers[i] = values[i]; } public IEnumerator GetEnumerator() { return new Enumerator(numbers); } }
After implementing the IEnumerator and the IEnumerable interfaces, you can then use the foreach loop. To start, you must prepare the collection and its items for processing. Here is an example: class Program { static int Main(string[] args) { double[] numbers = new double[5]; numbers[0] = 224.52; numbers[1] = 60.48; numbers[2] = 1250.64; numbers[3] = 8.86; numbers[4] = 1005.36; return 0; } } To enumerate the collection, declare a variable based on the implementer of the IEnumerable and pass the collection to its constructor. Once this is done, you can then user the foreach. Here is an example: using System; using System.Collections; public class Enumerator : IEnumerator { private double[] numbers; private int cur; public Enumerator(double[] list) { this.numbers = list; cur = -1; } public Object Current { get { return numbers[cur]; } } public void Reset() { cur = -1; } public bool MoveNext() { cur++; if (cur < numbers.Length) return true; else return false; } } public class Enumerable : IEnumerable { private double[] numbers; public void Identify(double[] values) { numbers = values; for (int i = 0; i < values.Length; i++) numbers[i] = values[i]; } public IEnumerator GetEnumerator() { return new Enumerator(numbers); } } class Program { static int Main(string[] args) { double[] numbers = new double[5]; numbers[0] = 224.52; numbers[1] = 60.48; numbers[2] = 1250.64; numbers[3] = 8.86; numbers[4] = 1005.36; Enumerable coll = new Enumerable(); coll.Identify(numbers); foreach (double d in coll) Console.WriteLine("Item {0}", d); ; return 0; } }
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|