IEnumerable vs ICollection vs IList vs IQueryable in C#

An article from www.knowledgescoops.com

Kunal Tandon
Mar 26, 2018 · 2 min read

In C#, we have been using collections like IEnumerable, ICollection, IList interchangeably but do you really know what is it that separates each other.

They all have specific characteristics that differentiate them and makes them adaptable to certain scenarios.

All of the above holds a list maintaining the order of items but which one should we use and in which case, let’s see.

IEnumerable vs ICollection vs IList vs IQueryable in C#

IEnumerable

Namespace: System.Collections

An IEnumerable is a list or a container which can hold some items. You can iterate through each element in the IEnumerable. You can not edit the items like adding, deleting, updating, etc. instead you just use a container to contain a list of items. It is the most basic type of list container.

All you get in an IEnumerable is an enumerator that helps in iterating over the elements. An IEnumerable does not hold even the count of the items in the list, instead, you have to iterate over the elements to get the count of items.

An IEnumerable supports filtering elements using where clause.

ICollection

Namespace: System.Collections

ICollection is another type of collection, which derives from IEnumerable and extends it’s functionality to add, remove, update element in the list. ICollection also holds the count of elements in it and we does not need to iterate over all elements to get total number of elements.

The count of total items can be retrieved in O(1) time.

ICollection supports enumerating over the elements, filtering elements, adding new elements, deleting existing elements, updating existing elements and getting the count of available items in the list.

IList

Namespace: System.Collections

IList extends ICollection. An IList can perform all operations combined from IEnumerable and ICollection, and some more operations like inserting or removing an element in the middle of a list.

You can use a foreach loop or a for loop to iterate over the elements.

IQueryable

Namespace: System.Linq

IQueryable extends ICollection. An IQueryable generates a LINQ to SQL expression that is executed over the database layer. Instead of the generating a Func<T, bool> like the ones above, IQueryable generates an expression tree and gives Expression<Func<T, bool>> that is executed over the database layer to get data set.



Want to read more articles like this?
Follow me on medium @kunaltandon.kt
Connect with me on LinkedIn:
https://www.linkedin.com/in/kunal-tandon/

Developer’s Arena

An arena for the developers in the city

Kunal Tandon

Written by

25k+ Monthly Views • 💁🏼‍♂️ Connect with me on LinkedIn @ http://bit.ly/kunal-tandon • Also Inviting people to sponsor your app/product in my articles

Developer’s Arena

An arena for the developers in the city

More From Medium

More from Developer’s Arena

More from Developer’s Arena

More from Developer’s Arena

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade