Introduction
When working with collections and data in C#, developers often come across three commonly used interfaces: IEnumerable, IQueryable, and ICollection. At first glance, they may look similar because all of them deal with collections of data. However, they serve different purposes and are used in different scenarios.
Understanding the difference between these three is very important, especially when working with databases, LINQ queries, and performance optimization.
In this article, we will break down each interface in simple words, understand how they work, and see the key differences with examples.
What is IEnumerable?
IEnumerable is the most basic interface used for iteration over a collection. It allows you to loop through a collection one item at a time.
Key Points of IEnumerable
Works with in-memory collections
Supports forward-only iteration
Uses LINQ to Objects
Executes queries immediately
Example of IEnumerable
IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
In this example, filtering happens in memory after the data is already loaded.
What is IQueryable?
IQueryable is more advanced and is mainly used when working with databases. It allows queries to be executed on the database side instead of in memory.
Key Points of IQueryable
Works with remote data sources (like databases)
Uses LINQ to Entities
Supports query translation to SQL
Executes queries lazily (deferred execution)
Example of IQueryable
IQueryable<Student> students = dbContext.Students;
var result = students.Where(s => s.Age > 18);
foreach (var student in result)
{
Console.WriteLine(student.Name);
}
Here, the filtering is done in the database, not in memory, which improves performance.
What is ICollection?
ICollection is used when you need more control over a collection. It allows you to add, remove, and count elements.
Key Points of ICollection
Supports modification (Add, Remove, Clear)
Provides Count property
Works with in-memory collections
Inherits from IEnumerable
Example of ICollection
ICollection<string> names = new List<string>();
names.Add("John");
names.Add("Alice");
Console.WriteLine(names.Count);
This interface is useful when you need to manage the collection directly.
Key Differences Between IEnumerable, IQueryable, and ICollection
| Feature | IEnumerable | IQueryable | ICollection |
|---|
| Execution | In-memory | Database-side | In-memory |
| Query Type | LINQ to Objects | LINQ to Entities | Not focused on querying |
| Performance | Slower for large data | Faster for large data | Depends on usage |
| Data Source | Local collection | Remote source (DB) | Local collection |
| Modification | No | No | Yes |
| Count Support | No direct property | No direct property | Yes |
Deferred Execution vs Immediate Execution
IEnumerable
IEnumerable uses deferred execution, but once data is fetched, all operations happen in memory.
IQueryable
IQueryable also uses deferred execution, but the query is not executed until it is actually needed. The major difference is that the query is translated into SQL and executed in the database.
Real-World Example
Using IEnumerable (Bad for Large Data)
var users = dbContext.Users.ToList();
var filteredUsers = users.Where(u => u.IsActive);
Here, all data is loaded first, then filtered.
Using IQueryable (Optimized)
var users = dbContext.Users.Where(u => u.IsActive);
Here, only required data is fetched from the database.
When to Use What?
Use IEnumerable When:
Use IQueryable When:
Use ICollection When:
You need to modify data
You need Count, Add, or Remove operations
You are managing collections directly
Common Mistake Developers Make
Many developers use IEnumerable instead of IQueryable when working with databases. This causes unnecessary data to be loaded into memory, leading to performance issues.
Understanding this difference can significantly improve application performance.
Conclusion
IEnumerable, IQueryable, and ICollection are all important interfaces in C#, but they serve different purposes.
IEnumerable is best for simple iteration over in-memory data
IQueryable is ideal for querying databases efficiently
ICollection is useful when you need to modify collections
Choosing the right interface can improve performance, readability, and scalability of your application.
By understanding these differences clearly, you can write better and more optimized C# code.