Could you explain on the following example what a deferred execution is and what materialization is? What will the output be?

string[] animals = { "dog", "cat", "crocodile", "Peter" };
var list = new List<string>(animals);
IEnumerable<string> query = list.Where(p => p.Length == 5);
list.Remove("Peter");
Console.WriteLine(query.Count());

Experience Level: Medior
Tags: .NETC#LINQPerformance

Answer

  1. The string array animals is initialized.
  2. New list of strings is created from the array using generic List<string> class.
  3. The query that matches all list items that have length 5 is assigned to the query variable. It's however not executed. The execution is deferred.
  4. The item "Peter" is removed from the list, which is perfectly valid.
  5. The call to .Count() will execute the evaluation of the query. The query execution was deferred until now but will be processed now.
  6. When the query gets evaluated, no items with length 5 exist in the list so 0 will be written as the output to the console.
  • Deferred execution is a term describing the fact, that the LINQ query is not executed immediatelly, but later.
  • Materializaton is the process of query evaluation and conversion to the real values (usually executed by iteration, .ToList(), .ToArray(), .Count() etc.)

Comments

No Comments Yet.
Be the first to tell us what you think.

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

RiceHawk18
e
RiceHawk18
@@xeDO0
RiceHawk18
1'"
RiceHawk18
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
RiceHawk18
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
RiceHawk18
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
RiceHawk18
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
RiceHawk18
VTgcz37T'; waitfor delay '0:0:15' --
RiceHawk18
1 waitfor delay '0:0:15' --
RiceHawk18
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
RiceHawk18
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
RiceHawk18
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
RiceHawk18
if(now()=sysdate(),sleep(15),0)
RiceHawk18
-1" OR 3+906-906-1=0+0+0+1 --
RiceHawk18
-1" OR 2+906-906-1=0+0+0+1 --
RiceHawk18
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 3+137-137-1=0+0+0+1 --
RiceHawk18
-1' OR 2+137-137-1=0+0+0+1 --
RiceHawk18
-1 OR 3+877-877-1=0+0+0+1
RiceHawk18
-1 OR 2+877-877-1=0+0+0+1
RiceHawk18
-1 OR 3+418-418-1=0+0+0+1 --
RiceHawk18
-1 OR 2+418-418-1=0+0+0+1 --
RiceHawk18
e
RiceHawk18
e