Expression delegate for dummies

  • Thread starter Thread starter wakefun
  • Start date Start date
W

wakefun

Guest
Dear all,

I have seen some sample code in asp.net core where they make the difference betwwen delegate and Expression delegate as sample code below :

// Lambda expression as executable code.
Func<int, bool> deleg = i => i < 5;
// Invoke the delegate and display the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));

// Lambda expression as data in the form of an expression tree.
System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
// Compile the expression tree into executable code.
Func<int, bool> deleg2 = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg2(4) = {0}", deleg2(4));

/* This code produces the following output:

deleg(4) = True
deleg2(4) = True
*/

I do not understand the benefit of using

System.Linq.Expressions.Expression

Could you please try to explain me with simple words what is the main advantage for using this complex reading syntax ?

Thanks fro feedback

regards

Continue reading...
 

Similar threads

Back
Top