Reputation: 87
The question about why IList<T>
doesn't inherit from IList
was asked before and answered.
The correct answer I believe was that if IList<T>
did inherit from IList
, then one could cast to IList and add items not of the correct type.
Here now comes my question.
Why does List<T>
implement both IList<T>
and IList
then? If you cast List<T>
to IList
and add items not of type T you correctly get an exception.
So what's the purpose of List<T>
implementing IList
?
Upvotes: 0
Views: 566
Reputation: 20730
then one could cast to IList and add items not of the correct type.
This is only a partial justification: It is well possible to implement IList
in a way so no items of an incorrect type can be added - as evidenced by the implementation that is List<T>
.
However,, List<T>
already exists as a part of the base class library. For our convenience, it implements IList
and throws exceptions. If we want to use its IList
implementation, we can do so within these constraints, and if we do not want to use it, we have no further work.
In contrast to this, IList<T>
is an interface. If it inherited from IList
, every implementor of IList<T>
would have to implement all of the weakly-typed methods of IList
, adding a lot of work that is often not desired.
Moreover, note that List<T>
implements IList
expilcitly. That means, the public interface of List<T>
does not grow; you only get the IList
methods if you explicitly cast to IList
. In an interface, that is not possible, as an interface cannot enforce explicit implementation of another interface.
Upvotes: 4