
At companies like Microsoft or Shopify, interviewers use this question to test whether you understand execution models, abstraction boundaries, and performance trade-offs in collection processing.
Explain the difference between IEnumerable and IQueryable in C#. Your answer should cover:
Also discuss deferred execution, expression trees vs delegates, and the practical impact on performance and maintainability.
The interviewer expects more than a definition. You should compare behavior under the hood, describe common use cases, mention typical pitfalls such as premature materialization, and explain why the wrong choice can lead to unnecessary memory usage or slower execution.
IEnumerable represents a sequence that is iterated in memory. LINQ operations on it are typically executed using delegates against objects already loaded into the application process.
var result = numbers.Where(x => x % 2 == 0);
IQueryable represents a query definition that can be translated by a provider before execution. Instead of immediately running logic in memory, it builds an expression tree that another system can interpret.
var result = source.Where(x => x.Age > 18);
Both interfaces often support deferred execution, meaning the query is not evaluated until iteration or materialization. This allows query composition, but it also means repeated enumeration may repeat work.
var filtered = items.Where(x => x > 10);
foreach (var x in filtered) { /* executes here */ }
IEnumerable-based LINQ usually uses compiled delegates such as Func<T, bool>, while IQueryable uses expression trees such as Expression<Func<T, bool>>. Expression trees preserve the structure of the query so a provider can analyze and transform it.
Expression<Func<User, bool>> predicate = u => u.IsActive;
Methods like ToList(), ToArray(), or First() force execution and often convert a deferred query into concrete results. Choosing where to materialize determines whether later operations run lazily or on fully loaded data.
var list = query.Where(x => x.Score > 90).ToList();