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

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

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

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

FeatureIEnumerableIQueryableICollection
ExecutionIn-memoryDatabase-sideIn-memory
Query TypeLINQ to ObjectsLINQ to EntitiesNot focused on querying
PerformanceSlower for large dataFaster for large dataDepends on usage
Data SourceLocal collectionRemote source (DB)Local collection
ModificationNoNoYes
Count SupportNo direct propertyNo direct propertyYes

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:

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.

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.