Enumerators can be an incredibly powerful way of iterating through data.
Iterators are also very useful in situations where the amount of data is not easily known at the start of the process.
- and for deserialisation of a file
The iterator reads the file, calls the necessary factory methods and passes back objects that it have been constructed based on data in the file.
using a while loop accessing the IEnumerator based object directly, and second, using a foreach loop accessing the enumerator through the IEnumerable interface.
As enumerators are always initially pointed to just before the first element, this index is set to -1. The Reset() method also sets the current index back to -1.
=========================
IEnumerator interface
The IEnumerator interface provides iterative capability for a collection that is internal to a class. IEnumerator requires that you implement three methods:
* The MoveNext method, which increments the collection index by 1 and returns a bool that indicates whether the end of the collection has been reached.
* The Reset method, which resets the collection index to its initial value of -1. This invalidates the enumerator.
* The Current method, which returns the current object at [position].
---------------------------
IEnumerable interface
The IEnumerable interface provides support for the foreach iteration. IEnumerable requires that you implement the GetEnumerator method.
Iterators are also very useful in situations where the amount of data is not easily known at the start of the process.
- and for deserialisation of a file
The iterator reads the file, calls the necessary factory methods and passes back objects that it have been constructed based on data in the file.
using a while loop accessing the IEnumerator based object directly, and second, using a foreach loop accessing the enumerator through the IEnumerable interface.
As enumerators are always initially pointed to just before the first element, this index is set to -1. The Reset() method also sets the current index back to -1.
=========================
IEnumerator interface
The IEnumerator interface provides iterative capability for a collection that is internal to a class. IEnumerator requires that you implement three methods:
* The MoveNext method, which increments the collection index by 1 and returns a bool that indicates whether the end of the collection has been reached.
* The Reset method, which resets the collection index to its initial value of -1. This invalidates the enumerator.
* The Current method, which returns the current object at [position].
---------------------------
IEnumerable interface
The IEnumerable interface provides support for the foreach iteration. IEnumerable requires that you implement the GetEnumerator method.
Comments
Post a Comment