Comparison between List, IList, and IEnumerable

29,590
  • IEnumerable<T> is the base interface that the following extend or implement. It doesn't allow for direct access and is readonly. So use this only if you intend to iterate over the collection.

  • ICollection<T> extendsIEnumerable<T> but in addition allows for adding, removing, testing whether an element is present in the collection and getting the total number of elements. It doesn't allow for directly accessing an element by index. That would be an O(n) operation as you need to start iterating over it until you find the corresponding element.

  • IList<T> extends ICollection<T> (and thus it inherits all its properties) but in addition allows for directly accessing elements by index. It's an O(1) operation.

  • List<T> is just a concrete implementation of the IList<T> interface.

In your code you should always expose the type that's highest in the object hierarchy that will correspond to the needs of the callers. So for example if the callers are only going to enumerate over the dataset, use IEnumerable<T>. If they need to have direct access to elements by index expose an IList<T>.

List<T> should only be used internally by your code but usually not present in the signature of the methods you are exposing. This gives you more flexibility as you could easily swap the concrete implementation without breaking the contract.

Share:
29,590
Lamloumi Afif
Author by

Lamloumi Afif

Hi, I am a Software Development Engineer with a genuine interest in .Net framework. I enjoy reading books and practising sport. LinkedIn Viadeo

Updated on April 17, 2020

Comments

  • Lamloumi Afif
    Lamloumi Afif over 3 years

    I have a C# application in which I handle some collection types. I need to know what the differences between these types are:

    1. List
    2. IList
    3. IEnumerable

    What are the differences for each one in comparison with the others?

  • Lamloumi Afif
    Lamloumi Afif almost 10 years
    thank you , it is clear. for the response time , when i try to find an element or sort the collection, what is the best of them?
  • Darin Dimitrov
    Darin Dimitrov almost 10 years
    Using LINQ you could achieve that with IEnumerable<T>.
  • Evonet over 9 years
    This is a great resource, where does Hashset fit it?
  • Erti-Chris Eelmaa
    Erti-Chris Eelmaa almost 9 years
    @Evonet: HashSet<T> implements ISet<T> which implements ICollection<T>.
  • nimish jain
    nimish jain almost 8 years
    @DarinDimitrov : Icollection doesn't allow add or remove.