Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't ICollection<T> implement ICollection? [duplicate]

Tags:

.net

generics

IEnumerable<T> implements IEnumerable.
But ICollection<T> does not implement ICollection.

What was the rationale for this and/or was it just an oversight?

like image 458
Sam Harwell Avatar asked Mar 01 '10 01:03

Sam Harwell


People also ask

Does ICollection implement IEnumerable?

ICollection<T> interface. The ICollection interface extends IEnumerable; IDictionary and IList are more specialized interfaces that extend ICollection.

What is the difference between ICollection and IList?

The IList interface implements both ICollection and IEnumerable interfaces. This interface allows us to add items to and remove items from the collection. It also provides support for accessing the items from the index. This interface has more power than the preceding two interfaces.

What is the difference between ICollection and IEnumerable?

IEnumerable has one property: Current, which returns the current element. ICollection implements IEnumerable and adds few additional properties the most use of which is Count. The generic version of ICollection implements the Add() and Remove() methods.


1 Answers

As Nick said, ICollection is pretty much useless.

These interfaces are similar only by their name, CopyTo and Count are the only properties in common. Add, Remove, Clear, Contains and IsReadOnly have been added while IsSychronized and SyncRoot have been removed.

In essence, ICollection<T> is mutable, ICollection is not.

Krzysztof Cwalina has more on this 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.

like image 150
Romhein Avatar answered Oct 30 '22 01:10

Romhein