Thursday, 1 March 2012

What is Lamda Expression how it is benefit the delegate?

Yes,delegates till C# 2.0 we perform the operations like pointer based works.I want to point a method no'f times we have to choose the delegate
Ex:

class AnonymousDemo
    {
       // Anonymous method is nothing but inline declaration of delegate;
        public delegate int Pointdata(int i,int j);
        static void Main()
        {
            Pointdata point = delegate(int k, int l)  // redundent signature.
                {
                    return k + l;
                };
            Console.WriteLine(point.Invoke(100, 20).ToString());
            Console.ReadKey();              
        }
        static int Add(int num1, int num2)
        {
            return num1 + num2;
        }      
    }
Now C# 3.0 onwords Lamda Expression is introduced in .net to simplify the task.
basic syntax is
   input paramters => expression/expressions
operator(=>) is called "goes to" operator. which is help full to us(developers) .Lamda Expression (=>) will
implictly creates an anonyms funcation to that delegate.like below
Ex:
class LamdaExperison
    {
        delegate int Add(int a, int b);
        static void Main()
        {         
            Add Addresult = (a, b) => a * b;
            Console.WriteLine(Addresult(10, 45).ToString());
            Console.ReadKey();      
        }
    }

as well as we have many generic delegates in .net  according to delegate signature.these delegates we have to use as return type good.

    class UsingGenericDelegate
    {    
        static void Main()
        {
            Func<int, int> obj = a => a + a;
            Console.WriteLine(obj(40).ToString());
            Console.ReadKey();              
        }

    }----->output: 80

**[but some types we can' t say the return type so we need to dependent on complier is good.

Complier will generate an apporiate expression dynamically(is called Expression Trees).

we have possible delegates

1.Func<int, int> square = x => x * x;
2. Func<int, int, int> mult = (x, y) => x * y;

but when runtime c# complier we desides the the type of delegate it has to choose based on the data. that is 1 or 2 of delegates.it dynamically during runtime and later on compile them to produce the actual anonymous delegates. In this article I will put forward the story of Linq and Lambda Expressions further and introduce you with Expression Trees and also show you to create Expression Trees dynamically during runtime.

Expression<Func<int, int, int>> multExpression =(x, y) => x * y;
       to
Expression.Lambda<Func<int, int>>(
    Expression.Multiply(x = Expression.Parameter(typeof(int), "x"), x),
    new ParameterExpression[] { x });

Here    (x, y) => x * y;---->is
Expression.Lambda<Func<int, int>>(
    Expression.Multiply(x = Expression.Parameter(typeof(int), "x"), x),
    new ParameterExpression[] { x });
lamda expression is replaced with an apporiate Expression Tree(anonymous delegate).


NOTE: Lambda expression into an expression tree.


No comments:

Post a Comment