In this article we will learn how to use the IEnumerator interface to allow you to use a foreach loop in a data set or collection.
Most collections (lists and others) already implement the interface, but in this case we will customize the way we browse through the list.
When we use code like this:
foreach(Class c in Collection)
{
...
}
The compiler convertes this code in something like this:
IEnumerator cc = Collection.GetEnumerator()
while(cc.MoveNext())
{
c=(Class)cc.Current;
...
}
Implementing the IEnumerable interface means that the class implements a version of the GetEnumerator () function that must return a class that implements the IEnumerator interface.
Let's explore an example.
We start with the client class:
This class will allow you to store customer data, and there is a field to indicate whether the customer is still active or not.
Next is a class that defines a client list and implements the IEnumerable interface that returns an object of type ListOfClientsEnum, which will be used in the foreach loop.
In the ListOfClientsEnum class we define how the list elements will be enumerated.
Since we only want to go through the active clients we will implement the IEnumerator interface.
We define a list of clients and a variable to store the position of the current element.
The class constructor sets a reference to the client list and sets the current position to -1, that is, before the first element.
The IEnumerator interface is required to implement the MoveNext, Reset functions, and the Current property as follows.
The Current property returns the currently selected element according to the position property.
The MoveNext function, in our case, advances to the next element, if it does not exist returns false.
The Reset function sets the position back to -1.
In the main function we use the class like this:
Note that listOfClients is an object and yet is used in the foreach loop to traverse clients using our ListOfClientEnum class.
Link to the complete project.
Most collections (lists and others) already implement the interface, but in this case we will customize the way we browse through the list.
When we use code like this:
foreach(Class c in Collection)
{
...
}
The compiler convertes this code in something like this:
IEnumerator cc = Collection.GetEnumerator()
while(cc.MoveNext())
{
c=(Class)cc.Current;
...
}
Implementing the IEnumerable interface means that the class implements a version of the GetEnumerator () function that must return a class that implements the IEnumerator interface.
Let's explore an example.
We start with the client class:
This class will allow you to store customer data, and there is a field to indicate whether the customer is still active or not.
Next is a class that defines a client list and implements the IEnumerable interface that returns an object of type ListOfClientsEnum, which will be used in the foreach loop.
In the ListOfClientsEnum class we define how the list elements will be enumerated.
Since we only want to go through the active clients we will implement the IEnumerator interface.
We define a list of clients and a variable to store the position of the current element.
The class constructor sets a reference to the client list and sets the current position to -1, that is, before the first element.
The IEnumerator interface is required to implement the MoveNext, Reset functions, and the Current property as follows.
The Current property returns the currently selected element according to the position property.
The MoveNext function, in our case, advances to the next element, if it does not exist returns false.
The Reset function sets the position back to -1.
In the main function we use the class like this:
Note that listOfClients is an object and yet is used in the foreach loop to traverse clients using our ListOfClientEnum class.
Link to the complete project.
Comentários
Enviar um comentário