Wednesday, August 10, 2016

Lamda Expression

Lambda expression is an anonymous function that you can use to create delegates or expression tree types

Func<double, double> cpointer = x => x * 3.123; //Input and Output
Action<string> action = y => Console.WriteLine(y); //Void
Predicate<int> GreaterThanFive = z => z > 5; //Input and Always return Bool

double area = cpointer(2);
action("action display");
bool isGreater = GreaterThanFive(4);

BinaryExpression b1 = Expression.MakeBinary(ExpressionType.Add, Expression.Constant(10), Expression.Constant(20));
BinaryExpression b2 = Expression.MakeBinary(ExpressionType.Add, Expression.Constant(5), Expression.Constant(3));
BinaryExpression b3 = Expression.MakeBinary(ExpressionType.Subtract, b1, b2);

int result = Expression.Lambda<Func<int>>(b3).Compile()();

No comments:

Post a Comment