Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is IList not deferred execution?

Tags:

c#

collections

As I understand it IEnumerable and IQueryable are deferred execution. Why wouldn't it be of benefit for IList to also support deferred execution?

like image 488
Joshua Enfield Avatar asked Sep 07 '11 17:09

Joshua Enfield


People also ask

Is IEnumerable deferred execution?

IEnumerable is suitable for LINQ to Object and LINQ to XML queries. IEnumerable supports deferred execution.

Which of these will cause deferred execution of a LINQ query?

LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution.

What is deferred execution using LINQ?

Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required. It greatly improves performance by avoiding unnecessary execution.

Could you explain what is the exact difference between deferred execution and lazy evaluation in C #?

Lazy means "don't do the work until you absolutely have to." Deferred means "don't compute the result until the caller actually uses it."


1 Answers

The longer I think about it, the more I question whether the whole idea of "deferred execution" is actually of pedagogic value at all.

I'll answer your question by denying it. IEnumerable<T>, IQueryable<T> and IList<T> do not in any way represent "deferred" or "eager" calculations.

Rather, interfaces represent an ability to provide a service. IEnumerable<T> represents the service "I can provide a sequence, possibly infinite, of items of type T, one at a time". IQueryable<T> represents the service "I can represent a query against a data source, and provide the results of that query on demand". IList<T> represents the service "I can provide random access to a possibly mutable, finite-sized list of items of type T".

None of those services say anything about the implementation details of the service providers. The provider of an IList<T> service could be entirely lazy; the provider of an IQueryable<T> service could be entirely eager. If you want to make a deferred-execution IList<T>, you go right ahead. No one is stopping you!

like image 117
Eric Lippert Avatar answered Sep 30 '22 19:09

Eric Lippert