IEnumerable<T>
implements IEnumerable
.
But ICollection<T>
does not implement ICollection
.
What was the rationale for this and/or was it just an oversight?
ICollection<T> interface. The ICollection interface extends IEnumerable; IDictionary and IList are more specialized interfaces that extend ICollection.
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.
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.
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 likeICollection
, but it’s actually a very different abstraction. We found thatICollection
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 thatICollection
does not have an exact corresponding peer in the generic world;IEnumerable<T>
is the closest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With