Thursday, 3 October 2013

Lamda Expression in C#

The concept of lamda expression was introduced in C# 3.0. It is just a new way to write anonymous methods. At compile time all the lamda expressions are converted into anonymous methods according to lamda expression conversion rules. The left side of the lamda operator "=>"(is called ‘goes to’ operator) represents the arguments to the method and the right side is the method body.

Lambda expression Syntax:
(parameters) => expression-or-statement-block

Types of Lamda expression

1. Statement Lamda
Statement lambda has a statement block on the right side of the lambda operator "=>".
Eg: a => {return a * a;};

       
2. Expression Lamda
Expression lambda has only an expression .Will not have a return statement or curly braces, on the right side of the lambda
operator "=>".
Eg: a => a * a; // here a*a is expression


Lets take an example :

Lets convert it to lambda expression

X=>x+2 is the lambda expression. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.

Hope you are enjoyed this.


No comments:

Post a Comment