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

Asked

Viewed 326 times

3

I tried to see the code of both interfaces, but the only difference I see is the IEnumerable<T> has the method IEnumerator<T> GetEnumerator();, and that at the interface IEnumerator<T> the Current returns the "T" instead of an Object... Would that be just to prevent the foreach from making a Unboxing after? or not? And another question, usually you do another class even to implement the interface IEnumerator and implements all methods Movenext, Reset, Current, etc... or just implement the IEnumerable<T> and return a Getenumerator() from the List is 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 the IEnumerator<T> even?

  • The standard and' implement the second method so: return GetEnumerator(); in order to avoid duplication of code.

1 answer

4


What’s the difference between IEnumerable<T> and IEnumerable?

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

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

...on the interface IEnumerator<T> the Current returns the "T" instead of the one object... That would be just to prevent the foreach from making one Unboxing after? or not?

No, actually the distinction is purely and simply for type security. Conversions that could arise midway could cause problems if the programmer tried conversions using different types.

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

If you just implement both GetEnumerator() already works. The method implementation the method is required if you want a behavior different from the known implementation standard.

  • Ah! Another thing I forgot to ask, because Ienumerator<T> implements Idisposable but not Ienumerator? It would be only to free space that the Ienumerator creates in foreach( in this case, the List/Vector/Matrix that it creates ) ?

  • For the same reason that there are two types. The point is that it’s a little different, actually. The foreach never checked whether the type in question implemented IDisposable until version 1.2. Only after that did you get the call to IDisposable after the foreach.

  • "No, actually the distinction is purely and simply by type security." - It’s not quite true. Generics were introduced not only to strengthen the type system, but also to avoid Boxing and improve performance.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.