David Klempfner
David Klempfner

Reputation: 9870

Why use ICollection<T> over IList<T>?

I've read articles that say use ICollection<T> if you want the functionality of IEnumerable<T>, but also want the Count property.

However since the extension methods in System.Linq.Enumerable provide methods such as ElementAt(), why wouldn't you just use an IList<T> instead, since that uses an indexer?

Seems to me that ICollection<T> can do what IList<T> can do, just in a much more verbose and less readable way.

What scenario would it be more readable/more efficent, more adhering to SOLID principles, or some how better in any way to use ICollection<T> over IList<T>?

EDIT:

The duplicate question's answers do not answer my question since they avoid talking about the fact that ICollection<T> can do what IList<T> does but in a much uglier way.

If they both do the same thing, why not just use the cleaner IList<T>?

I received much better answers here: https://www.reddit.com/r/csharp/comments/dl9xao/why_use_icollectiont_over_ilistt/

Upvotes: 1

Views: 1437

Answers (3)

pim
pim

Reputation: 12577

If you're looking at this from an overhead perspective, I think you're best bet is the intermediary between the two, ISet<T> and it's implementation of HashSet<T>.

Set's are often overlooked, but are extremely efficient in determining "ownership" and save space by virtue of not relying on keys since they don't concern themselves with ordering.

EDIT (2020/11/18) - It's important to note that upon adding an item to the HashSet, it's HashCode will be computed. The cost of this might be expensive, and thus complete negate any performance benefits you might gain from faster lookups.

The important difference is that Add(), Remove() and Contains() all become o(1) (vs the typically o(n) cost of IList<T>.

There is also SortedSet<T> if ordering becomes an issue.

Here's a great article explaining this is much better depth than I can. An important snippet:

The HashSet<T> is the set of choice if you want the fastest possible lookups but don’t care about order. In contrast the SortedSet<T> will give you a sorted collection at a slight reduction in performance.

Upvotes: 2

Code Name Jack
Code Name Jack

Reputation: 3293

The answer is highly subjective. It's more about how much you need.

  • If all you need is to loop through all the items, prefer IEnumerable<T>
  • If you need to manupulate the collection (Add, Remove etc.) ICollection<T> should be used.
  • If you are going to use Index frequently, use IList<T>.
  • If you are planning to do all sort of manipulation go ahead and use List<T>.

However in those cases, there are IReadonlyCollcetion<T> and IReadonlyList<T>, this makes the returned collection immutable and again should come after IEnumerable but preferred over ICollection<T> and IList<T>.

The end goal always is to use as abstract types as possible. This is based on the principles Code for the interfaces not for the implementation.

Edit: For this question I would say need of indexing is the primary factor of preferring one over other.

Upvotes: 2

Fran
Fran

Reputation: 351

The short response is that you can just use IList for any list of elements. In fact, IList implements ICollection and ICollection implements IEnumerable. You're able to decide what level of abstraction you need according to the context.

See this link for details.

Upvotes: -1

Related Questions