Someone could explain in a more didactic way what these interfaces are for
IEnumerable is an interface that marks the classes that want to implement it so that it is known that it can be iterated through an iterator. Obviously this should only be used on objects that are data strings, otherwise it does not make sense to iterate there. The method is used to get the iterator. Most of the time the implementation of this method is the same or very similar.
Whenever you implement
IEnumerable you will have to implement the
IEnumerator interface in some way, in the class or elsewhere.
The iterator is defined by the
IEnumerator interface that proposes to have the mechanism required to iterate, ie it needs a state with the current object (
Current ) and how to restart the iterator scan (
What advantages do they bring to iterations that a "simple loop" does not bring?
If the language does not determine how to loop in a data stream, the only way you can create a type that is an iteratable data stream is by providing a mechanism that tells you how to get the data, the next one is how to start over . Your type should provide the code that does this appropriately and if possible with the best possible performance for your case. In the case the language does in array and string , then in thesis these types would not need to have these interfaces, but they have to use in other situations that the language does not control. / p>
This is not quite true. It is even possible in most cases not to implement
Reset() in a collection, but a lot of things will not be possible. Nor can we consider it a collection, at least not an iterable one. It will be a beauty of a gambiarra. It has collection that simply can not iterate without doing something totally non-standard. And the purpose of these interfaces is to maintain a standard way of doing things.
Can anyone explain to me why this code works even though it is not the most commonly taught code for implementing IEnumerator?
The code works because that's what it has to do.
- it takes the iterator and throws it into an object
IEnumerable takes the next element of the sequence, in case it should get the first
- accesses the element within the loop
- Return to check if you have the next item to decide whether to continue or not, since
MoveNext() returns a Boolean.
How this works internally is the specific iterator problem, it's implementation detail.
In fact this code is not good because it leaks memory leaving the iterator alive even after it has finished its use. In other situations it is possible for a cast to be required to use the element since it returns a
MoveNext() , unless it uses a
object to return the actual object type. All this the compiler puts for you, so it's best to use
IEnumerator<T> of what to do in the hand, the compiler knows what it is doing, the programmer does not always.
foreach can only work on objects that have the
foreach interface since it must iterate in the data sequence.
If .NET were written today would be different, perhaps the idea of Jared Parsons was the implementation, which is much better than the one adopted.