Why IEnumerable<T> inherits from IEnumerable?

  • Thread starter Thread starter Morgan Cheng
  • Start date Start date
M

Morgan Cheng

In .Net 2.0, Generics are introduced. I found that IEnumerable<T>
inherites from IEnumerable. This makes implementation of
IEnumerable<T> has to have two GetEnumerator methods defined( one for
IEnumerable<T> and the other for IEnumerable). I doubt why .Net class
hierarchy is designed in such a way. IMHO, they should not have
inheritance releationship, just like IList<T> and IList.

I googled the web and found two related articles.
http://www.wintellect.com/cs/blogs/...2/13/ienumerable-lt-t-gt-and-ienumerable.aspx
http://blogs.msdn.com/brada/archive/2005/01/18/355755.aspx
But, the explanation is not satisfying.

Even though "IEnumerable<T> inherits from IEnumerable" seems follow
Liskov Substitution Principle, I believe, LSP is just a necessary
condition, not sufficent condition. And, If IEnumerable<T> doesn't
inherit from IEnumerable, no old code would be broken.

Anybody can shed some light on that?
 
Even though "IEnumerable<T> inherits from IEnumerable" seems follow
Liskov Substitution Principle, I believe, LSP is just a necessary
condition, not sufficent condition. And, If IEnumerable<T> doesn't
inherit from IEnumerable, no old code would be broken.

But the old code wouldn't be as useful. Currently, if I have a method
which takes IEnumerable, and I have an object which implements
IEnumerable<T>, I can use the two together without any extra work. It
would have been possible to make a proxying class to convert from one
to the other, of course, but I don't think the pain involved is that
significant here.
Anybody can shed some light on that?

I thought the explanation from Anders as quoted by Brad was pretty
good, myself.

Jon
 
In my opinion, the inheritance decision comes from the interoperability (COM
interoperability, for example, because COM interop cannot access generics
directly, they can access the standard IEnumerable.
http://msdn2.microsoft.com/en-us/library/ms229590.aspx). And not all
languages have support for generics.
Also because an enumerable object is usually a compiler-generated enumerable
class so the double implementation is not much an issue.
I don't see anything exceptionally wrong with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top