What is the difference between IEnumerableT and IEnumerable?

3

I tried to see the code of both interfaces, but the only difference I see is IEnumerable<T> has method IEnumerator<T> GetEnumerator(); , and that in interface IEnumerator<T> Current returns "T" instead of object ... Would this be just to prevent the foreach from doing an unboxing afterwards? or not? And another question, do you usually make another class even to implement the interface IEnumerator and implement all the methods MoveNext, Reset, Current, etc ... or just implement IEnumerable<T> and return a GetEnumerator () from List already enough? Example:

public IEnumerator<Error> GetEnumerator()
{
    return this._errors.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
    return this._errors.GetEnumerator();
}

GetEnumerator() is an Array Class method that returns to us a class that implements IEnumerator<T> same?

    
asked by anonymous 13.04.2015 / 06:53

1 answer

4

What is the difference between IEnumerable<T> and IEnumerable ?

In theory there should not be such a distinction. The problem is that .NET 1.0 was first implemented IEnumerable . Generic types came only from .NET 2.0.

Without delays, IEnumerable basically returns iterators of type Object , and IEnumerable<T> returns typed iterators.

  

... in the interface IEnumerator<T> o Current returns the "T" instead of   a object ... This would only be to prevent the foreach from making a   unboxing after? or not?

No, in fact the distinction is purely and simply for type security. Conversions that might arise in the middle of the path could cause problems if the programmer attempted conversions using different types.

  

And another question, is usually another class to implement   the IEnumerator interface implements all MoveNext methods,    Reset , Current , etc ... or just implement IEnumerable<T> e   return a GetEnumerator() of List already enough?

If you only implement the two GetEnumerator() already works. Method-to-method implementation is required if you want behavior other than the default one from the known implementation.

    
13.04.2015 / 07:07