As I understand it IEnumerable and IQueryable are deferred execution. Why wouldn't it be of benefit for IList to also support deferred execution?
IEnumerable is suitable for LINQ to Object and LINQ to XML queries. IEnumerable supports deferred execution.
LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution.
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.
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."
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With