Friday, June 19, 2009

Lambda Expression: Basic and benefits

Generally, a Lambda Expression is anonymous function that use lambda expression (=>) which is read as "goes to". The format of the lambda expression is as follows:

parameters => body

Here if number of parameters is one the parenthesis is optional. If more than one parameters are used then parameters need to inside parenthesis. Also if the number of statements in body part is one then no second bracket is needed but if number of statement is more than one then statements need to inside second bracket. In the following code snippet I have declared two delegates. One represent integer binary operation signature and another is for unary operation signature.


The delegate with unary integer operation signature is as follows:

delegate int DelUnary(int i);

In the above statement the delegate name is DelUnary and it taken an integer value and return an integer value.


The delegate with binary operation signatue is as follows which takes two integer value and return an integer value:

delegate int delBinary(int t, int x);


Now lets talk about unary delegate. We need two methods for incrementing and decrementing values. So we can define these two functions and assign those two functions in a variable of type DelUnary as shown below:

            DelUnary increment = x => x++;

            DelUnary decrement = x => {x--};

In the above statement we have declared an variable (increment , decrement) of type DelUnary and then we have defined the body of the methods (increment, decrement). The body start after equal sign (=). The body part (x=x++;) or (x=>x--) has two parts. The left part of lambda operator (=>) is parameter (i.e., x) and right part of the lambda expression is the method body. The body may contains multiple statements but its recommended not to be more than 2/3 lines. In case of multiple statements you need to use curly braces({}) for enclosing statements.

Now we can call the method as

            int i=0;




Similarly we can declare binary functions and call them as shown below:

            delBinary multiplication = ((x, y) => x * y);

            delBinary summation = ((x, y) => x * y);

            delBinary subtraction = ((x, y) => x * y);


            int m = 5;

            int n = 10;

            Console.WriteLine(multiplication(m, n));

            Console.WriteLine(subtraction(m, n));

            Console.WriteLine(summation(m, n));


Lambda expression allows us to write small code to do more. Lets say we have a collection of names and we need to find person whose name start with "mar". So in conventional C# we write something as shown below:


        string[] names = { "smith", "john", "mike", "mark", "seth", "maria", "manson" };

        IList<string> foundNames= new List<string>();

        foreach (string name in names)


            if (name.StartsWith("mar"))






But with lambda expression we can easily shorten the above code into small and more neat form as shown below:

string[] names = { "smith", "john", "mike", "mark", "seth", "maria", "manson" };

IList<string> foundNames = names.Where(s => s.StartsWith("mar")).ToList();

Isn't that goo huh? I like this coding style very much and I have used this style in my few new projects. Believe me I'm enjoying this coding style. When I started using lambda expression for the first time it was a kind of wired stuff to me. But as I'm exploring I'm finding this really interesting and I personally believe that it will increase programmer productivity as programmer need to write lees code.

Next time I'll explain Lambda Expression Tree which is also interesting.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.