Introduction
When working with collections in C#, you will often come across IEnumerable, ICollection, and IList. They may look similar, but they represent different levels of functionality and performance. Choosing the right one can help you write cleaner, faster, and more maintainable code. In this article, you will learn what each one means, how they differ, and when to use them in real-world C# applications. All explanations are written in simple words, supported with practical examples.
Understanding IEnumerable in C#
IEnumerable is the most basic and lightweight interface for working with collections. It allows you to loop through items using a foreach loop. But you cannot add, remove, or update items using IEnumerable.
Key Points of IEnumerable
Supports only iteration (read-only access).
Great for LINQ queries.
Does not support adding or removing items.
Best when you only need to loop through data.
Example
IEnumerable<string> names = new List<string>() { "John", "Emma", "Alex" };
foreach (var name in names)
{
Console.WriteLine(name);
}
Here, you can loop through the items but cannot modify the list because IEnumerable provides read-only access.
Understanding ICollection in C#
ICollection builds on top of IEnumerable and adds more capabilities. It allows you to add, remove, and count items. It is still a general-purpose interface, but more powerful than IEnumerable.
Key Points of ICollection
Inherits from IEnumerable.
Allows adding and removing items.
Provides Count property.
Supports operations like Contains, CopyTo, and Remove.
Example
ICollection<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Remove(10);
Console.WriteLine(numbers.Count); // Output: 1
ICollection lets you modify the collection while still allowing iteration.
Understanding IList in C#
IList is the most powerful interface among the three. It extends both ICollection and IEnumerable, and additionally supports index-based access like arrays.
Key Points of IList
Inherits from ICollection and IEnumerable.
Allows adding, removing, and updating items.
Allows accessing items by index (list[0]).
Best when you need full collection control.
Example
IList<string> cities = new List<string>() { "Delhi", "Mumbai" };
cities.Add("Kolkata");
Console.WriteLine(cities[0]); // Output: Delhi
cities[1] = "Pune"; // Updating value using index
You get full access to modify or retrieve items using an index.
Difference Between IEnumerable, ICollection, and IList
1. Functionality
IEnumerable: Read-only, iteration only.
ICollection: Add, remove, count, and iterate.
IList: Everything in ICollection + index-based access.
2. Performance Impact
IEnumerable is fastest for reading data.
ICollection adds overhead for modification operations.
IList provides flexibility but may have performance costs when frequently inserting or removing items.
3. When to Use What?
Use IEnumerable when you need simple iteration and no modification.
Use ICollection when you need to modify the collection but do not care about index access.
Use IList when you need full access including indexing, updating, or retrieving items by position.
Real-World Examples
When to Use IEnumerable
public IEnumerable<Product> GetProducts()
{
return _context.Products.Where(p => p.Price > 100);
}
When to Use ICollection
public ICollection<string> GetNames()
{
return new List<string>() { "Amit", "Ravi", "Sana" };
}
When to Use IList
public IList<int> GetIdList()
{
return new List<int>() { 1, 2, 3, 4 };
}
Conclusion
IEnumerable, ICollection, and IList may look similar, but each serves a different purpose. IEnumerable is perfect for read-only operations and efficient looping. ICollection adds the ability to modify the collection, and IList gives you complete control with index-based access. Choosing the right interface depends on your project needs — whether you focus on performance, read-only access, or full manipulation features. With a clear understanding of these interfaces, you can write better, cleaner, and more efficient C# code.