Skip to main content

Command Palette

Search for a command to run...

What is the Difference Between IEnumerable, ICollection, IQueryable, IList, and List in C#?

Published
3 min read

Introduction

When working with collections in C#, we often come across IEnumerable, ICollection, IQueryable, IList, and List. Each serves different purposes and has distinct functionalities. Let’s break them down and understand when and why to use each.


IEnumerable

IEnumerable is used to represent a collection of elements that can be iterated (enumerated). It is read-only, meaning you cannot add or remove items, and it doesn’t support indexing. This interface is ideal for situations where you only need to loop through the collection using foreach similar constructs.

Key Points:

  • Supports enumeration (iteration).

  • Cannot add or remove items.

  • Does not support indexing.

  • Best for performing simple iterations.

Example:

IEnumerable<int> numbers = new List<int> { 1, 2, 3 };
foreach (var number in numbers)
{
    Console.WriteLine(number);
}

ICollection

ICollectionextendsIEnumerable and provides additional functionality for working with collections of varying sizes. Unlike IEnumerable, it allows adding and removing elements, getting the count of items, and checking for the existence of an element. However, it does not support direct indexing.

Key Points:

  • Extends IEnumerable with more functionality.

  • Allows adding and removing items.

  • Supports counting and checking for existence.

  • Does not support direct indexing.

Example:

ICollection<int> collection = new List<int> { 1, 2, 3 };
collection.Add(4);
collection.Remove(2);
Console.WriteLine(collection.Count);

IList

IListbuilds on ICollection by adding support for direct indexing. It allows you to access, add, remove, or insert elements at specific positions. This makes it highly flexible when working with ordered collections.

Key Points:

  • Extends ICollection with indexing capabilities.

  • Allows adding, removing, and inserting at specific positions.

  • Supports direct access to elements using an index.

Example:

IList<int> list = new List<int> { 1, 2, 3 };
list[1] = 5; // Updates the element at index 1.
list.Insert(2, 4); // Inserts 4 at index 2.
Console.WriteLine(list[2]);

IQueryable

IQueryable is a powerful interface used for querying collections with LINQ. It supports deferred execution, meaning queries are only executed when the results are accessed. It is commonly used with remote data sources like databases, as it can translate LINQ queries into SQL for execution.

Key Points:

  • Extends⁣IEnumerable with LINQ querying capabilities.

  • Supports deferred execution.

  • Commonly used with ORMs like Entity Framework for querying databases.

Example:

IQueryable<int> queryable = dbContext.Numbers.Where(n => n > 2);
var results = queryable.ToList(); // Executes the query and fetches results.

List

List is a concrete implementation of IList and ICollection. It provides all the functionality of IList along with dynamic resizing. Lists are commonly used for in-memory collections that require modification, indexing, and dynamic growth.

Key Points:

  • Implements IList and ICollection.

  • Supports dynamic resizing.

  • Ideal for in-memory collections that require frequent modifications.

Example:

List<int> numbers = new List<int> { 1, 2, 3 };
numbers.Add(4);
numbers.RemoveAt(1);
Console.WriteLine(numbers[0]);

Summary

To summarize:

  • IEnumerable is best for iterating through collections.

  • ICollection adds the ability to modify and count elements.

  • IList is perfect when indexing and position-based operations are required.

  • IQueryable is used for querying data from remote sources like databases using LINQ.

  • Listprovides a concrete implementation with all the features of IList and dynamic resizing.

By understanding the differences between these interfaces and classes, you can choose the most appropriate one for your specific use case.


Feel free to share this guide with your fellow developers to help them understand these essential C# concepts!


Attributions

And specifically, I would like to thank @osupersunny, my tutor, for the great work done for me.