What is IEnumerable and IEnumerator?

3

I've done a combo of questions about IEnumerable and IEnumerator to not only help me but everyone who needs a more didactic explanation.

Question 1

  

Someone could explain in a more didactic way what these   interfaces and what advantages they bring to iterations that a   "simple loop" does not bring?

Question 2

  

Given that IEnumerable only has one method that calls the   IEnumerator and that to iterate a collection we do not have the obligation   of implementing IEnumerable, a question follows:    What is the relationship of IEnumerable to IEnumerator?

Question 3

  

Can someone explain to me why this code works even though it is not   the most commonly taught code for implementing IEnumerator?

//IEnumerator
      int[] array = new int[]{1, 2, 3, 4, 5 }; 
      IEnumerator o = array.GetEnumerator();
      while (o.MoveNext())
      {
        Console.WriteLine(o.Current);    
      } 

About the foreach relation with IEnumerator / IEnumerable

You can get a lot of information here .

    
asked by anonymous 21.03.2017 / 17:24

2 answers

3
  

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 ( MoveNext() ).

  

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.

The 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.

    
21.03.2017 / 17:55
3
In Question 3 I believe that because arrays already implement the "factory" IEnumerable we are able to "instantiate" the enumerator through the array.GetEnumerator() method and its consequent assignment to the object IEnumerator o .

  

We must take into account that the use of the IEnumerator in this case is   only for the purpose of study because it is very clear that the best   is to use a "simple loop" (   for / while or even the foreach).

    
22.03.2017 / 11:38