- IEnumerable<> => Allows use of 'foreach' on a collection. The collection can be inspected in a forward only manner. You must never remove or add elements of an enumerable whilst iterating it.
- ICollection<> => As IEnumerable<> + Add(), Remove(), Count, Clear(), Contains(), IsReadOnly, CopyTo()
- IList<> => As ICollection<> + this[int], Insert(), IndexOf(), RemoveAt(). ie. It adds indexing type list operators.Use this when you need a collection that is considered ordered in some manner
- IReadOnlyXXX<> => Finally we have the ReadOnlys: IReadOnlyList<> and IReadOnlyCollection<>. Most of the time when you return a Collection or List you don't want your users to be able to modify it. This is where the ReadOnly modifier comes into play. Apart from the IEnumerable<> which is at the heart of Linq and all it's wonderful magic, most of the time you will be probably be returning an IReadOnlyXXX<>
February 2, 2008
IEnumerable, ICollection, IList Compared
Subscribe to:
Post Comments (Atom)
1 comment:
Just what I was looking for, a concise comparison. Thanks.
Post a Comment