Usage of Lambda Operator (=>) in C# (C Sharp)Lambda Operator (=>) is provided by C# version 3.0. This operator is very helpful when you use anonymous methods in your code or when you deal with lambda expressions.
Usage of Lambda Operator in Delegates: Consider the following example illustrating the usage of delegates with anonymous methods: public delegate
bool testDelegate(int param1, int param2); public static
void Main( ) { In this example, you have a delegate accepting two integer parameters and returning a Boolean value. This Boolean value is interpreted in printDelegateResult function and corresponding text message is populated. This message is then printed in Main( ) method. In Main( ) method, you create an instance of the delegate by defining anonymous method. You then pass this delegate object to printDelegateResult method. Creating delegate instance and passing it to printDelegateResult is done in two statements. You can simplify it further as shown below: public static
void Main( ) { In this example, you have clubbed the anonymous method definition as well as the printDelegateResult call in a single statement. This is interesting but the statement looks verbose and it is easily error prone. To make things easier, C# 3.0 provides lambda operator using which the above code can be modified as shown below: public static
void Main( ) { This statement
inside Main( ) method might look strange for you. During execution, The operator => is the lambda operator. Left hand side of the lambda operator contains the input variables of the method and right hand side of lambda operator is the method body. This example illustrates usage of lambda operator in delegate with parameters. What if your delegate doesnt contain any parameters? Then the left hand side of lambda operator will contain empty brackets ( ) as shown below: delegate
void sampleDelegate( ); In this example, the left hand side of lambda operator has ( ) to indicate that the delegate has no parameters. Also note that the right hand side of the lambda operator includes a method call rather than defining anonymous method. This is also permissible. Usage of Lambda Operator in Lambda Expressions: So far you have dealt with the usage of lambda operators to handle anonymous methods in delegates. Yet another usage of lambda operators is in lambda expressions. Lambda expressions look very alike to anonymous methods. In fact they are more flexible than the later. Lambda expressions are expressions used to create and manipulate expression tree types. Here is a simple example illustrating the usage of lambda operator in lambda expressions to handle expression tree types: delegate
int sampleDelegate(int param); Here the expression is constructed using lambda operator which provides the parameter and method body to be supplied for the delegate associated with the expression. In both delegates and lambda expressions, the left hand side of lambda operator contains just the names of the parameters and not their types. This is because the compiler can deduce the type of these parameters. However when you feel that it is difficult for the compiler to interpret, you can explicitly specify the type of the parameters. The statement in Main( ) method of the above example can also be specified as: System.Linq.Expressions.Expression<sampleDelegate> sampleExp = (int param) => param * param;
|