Difference between IEnumerable, ICollection, IList and IQueryable. Did you know?
Did you know the difference between IEnumerable, ICollection, IList and IQueryable? Often we implement these interfaces to use with Collections, without understanding the interfaces. According to the Interface Segregation principle (ISP)1, it is better to have (and use) more specific interfaces than general-purpose interfaces.
Let’s have a look at the interfaces, their contracts and specific characteristics.
Interfaces
Depending on the purpose of the implementation you are after, the different interfaces provide flexibility to choose. The fatter the interface, the more maintenance is to keep.
IEnumerable
This interface only allows the iteration over the elements in a collection. It comes from the namespace System.Collections.
With Linq, it uses Linq to Objects, which means that all objects are loaded into memory from database, independent of the query. If filtering is applied to the original query, it is only executed after the objects are already loaded in memory.
| |
ICollection
This interface extends IEnumerable and also allows the modification of collections. It comes from the namespace System.Collections.
| |
IList
This interface extends IEnumerable and ICollection. It allows the manipulation of the collection, the positioning and the order of the collection. It comes from the namespace System.Collections.
| |
IQueryable
This interface extends IEnumerable. It comes from the namespace System.Linq.
With Linq, it converts Linq expression to SQL statement, by applying filters at the database layer and returning filtered records.
| |

Join the conversation! Share your thoughts and connect with other readers.