Thursday, 1 November 2012

Lamda Expressions simplifies the coding?


Lamda Expressions simplifies the coding?
Yes, Lamda expression simplifies the coding …
Lamda expression return type should by a delegate type.
Delegate =from n in lamda expression.

CASE -I
Expression<Func<int, int, int>> expression1 = (x) =>x * x;
int value1 = expression1.Compile().Invoke(4, 4);



treates as Expression type

public sealed class Expression<TDelegate> : LambdaExpression
{

      public  TDelegate  Compile();
}

so 

expression.Complie() -----> returns also TDelegate

incase of invoke based on type of delegate which take paramters

like

int value=expression1.complie().invoke(4);

               or

Fun<int,int> expr=expression1.complie();
int val=expr.invoke(4);
console.write(val);

Because of
Expression1.Compile()-à consider as delegate
so invoke() method.

CASE-II

Func<int, int, int> expression2 = (a, b) => a * b;
int value2 = expression2.Invoke(41, 424);



treates as Delegate type

we know that delegate has Invoke method of

Console.WriteLine(value1);---17384
           
Console.WriteLine(value2);---17384

HERE output same

System.Linq.Expressionsà contains

public abstract class Expression
{
protected Expression(ExpressionType nodeType, Type type);
public ExpressionType NodeType { get; }
public Type Type { get; }
BindaryExpression,
UnaryExpression,
 MethodCallExpression……ect
}        

ExpressionType: Add,Multiple,Substract..ect
Type: int,string,…..ect.
For initialize the Expression tree.

ExpressionàBody and Paramters

Body----implementsà Binary Expression/Uniary Expression/Constant Expression..ect
Based on the type Body expression parameters will changes.


As well based on Binary operation NodeType also changes.

NodeType: Add, Multiple,Subtract,Devide…ect

So all the properties are belogs to BindaryExpression.

Ex:

How to build expression tree?

Expression tree= Body+Paramters.

Demo 1: building BinaryExpression 

Parametersà

  ParameterExpression parameter1 = Expression.Parameter(typeof(int), "x");
Body------à
  BinaryExpression multiply = Expression.Multiply(parameter1, parameter1);
  Expression<Func<int, int>> square = Expression.Lambda<Func<int, int>>(
             multiply, parameter1);

Expression static class defines : Expression<TDelegate> Lambda<TDelegate>(


Func<int, int> lambda = square.Compile();
Console.WriteLine(lambda(4));------> 16

// return type should be Apporiate delegate type          // Fun<int,int>

Or

int lamda=square.Compile().Invoke(4);----->16
Console.WriteLine(lambda);

so

ParameterExpression parameter1 = Expression.Parameter(typeof(int), "x");
BinaryExpression multiply = Expression.Multiply(parameter1, parameter1);
Expression<Func<int, int>> square = Expression.Lambda<Func<int, int>>(multiply, parameter1);
Func<int,int> lamda= square.Compile();
Console.WriteLine(lamda(4));




this is simplified like 

Func<int, int, int> expression2 = (a, b) => a * b;
int value22 = expression2.Invoke(41, 424);
Console.WriteLine(value22);



Here square is a Delegate of type which takes 2 parameters.so return type also a delegate which takes same structure.

public static Expression<TDelegate> Lambda<TDelegate>(Expression body, IEnumerable<ParameterExpression> parameters);
public static Expression<TDelegate> Lambda<TDelegate>(Expression body, params ParameterExpression[] parameters);

advantages:

we can reduce the no'f method to declaring.

based on the signature of method which will execute.

Demo 2: UnaryExpression




references:
http://weblogs.asp.net/dixin/archive/2009/11/29/understanding-csharp-3-0-features-6-lambda-expression.aspx

http://www.dotnetfunda.com/articles/article1956-func-and-action-delegates.aspx

No comments:

Post a Comment