Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Why didn't Microsoft make a ReadOnlyCollection<T> inherit from the ReadOnlyCollectionBase?

Simply put, Microsoft defined a ReadOnlyCollectionBase, yet did not use it as the base class for ReadOnlyCollection<T> when it clearly sounds that this should have been the way.

Am I missing something here? I mean, was there a good reason to NOT make this class the base class?

like image 270
myermian Avatar asked Jun 21 '11 18:06

myermian


1 Answers

Probably because it's not generic and implements ICollection whereas ReadOnlyCollection<T> is generic and implements ICollection<T>. Note that ICollection<T> does not implement ICollection. On that topic:

ICollection<T> seems like ICollection, but it’s actually a very different abstraction. We found that ICollection was not very useful. At the same time, we did not have an abstraction that represented an read/write non-indexed collection. ICollection<T> is such abstraction and you could say that ICollection does not have an exact corresponding peer in the generic world; IEnumerable<T> is the closest.

From that same post:

ReadOnlyCollection<T> is a much better ReadOnlyCollectionBase. It’s in System.Collections.ObjectModel namespace.

Effectively, ReadOnlyCollection<T> is ReadOnlyCollectionBase in the generics space.

Note, similarly, that IList<T> does not implement IList.

In general, pre-generics classes do not serve as useful abstractions for generic classes (with the exception of IEnumerable).

like image 85
jason Avatar answered Oct 16 '22 16:10

jason