Comparison between List, IList, and IEnumerable

C#.Net

C# Problem Overview


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] 1
  2. [IList] 2
  3. [IEnumerable] 3

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

C# Solutions


Solution 1 - C#

  • 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.

Solution 2 - C#

IList

IList exists in System.Collections Namespace.

IList is used to access an element in a specific position/index in a list.

Like IEnumerable, IList is also best to query data from in-memory collections like List, Array etc.

IList is useful when you want to Add or remove items from the list.

IList can find out the no of elements in the collection without iterating the collection.

IList supports deferred execution.

IList doesn't support further filtering.

IEnumerable

IEnumerable exists in System.Collections Namespace.

IEnumerable is a forward only collection, it can't move backward and between the items.

IEnumerable is best to query data from in-memory collections like List, Array etc.

IEnumerable doen't support add or remove items from the list.

Using Ienumerable we can find out the no of elements in the collection after iterating the collection.

IEnumerable supports deferred execution.

IEnumerable supports further filtering.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLamloumi AfifView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#vilas sagarView Answer on Stackoverflow