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);
class testDelegateClass {
public string printDelegateResult(testDelegate deleObj, int param1, int param2)
{
string resultString = null;
bool resultVal = deleObj(param1,param2);
if(resultVal) {
resultString = “First Parameter of Delegate is Greater Than or Equal To Second
Parameter”;
}
else {
resultString = “First Parameter of Delegate is Less Than Second Parameter”;
}
return resultString;
}

public static void Main( ) {
testDelegate deleObj = delegate(string param1, string param2){
return param1 >= param2;
};
string resultString = printDelegateResult(deleObj, 10, 20);
Console.WriteLine(resultString);
}
}

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( ) {
string resultString = printDelegateResult(
delegate(string param1, string param2){
return param1 >= param2;
}, 10, 20);
Console.WriteLine(resultString);
}

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( ) {
string resultString = printDelegateResult((param1, param2) => param1 >= param2, 10, 20);
Console.WriteLine(resultString);
}

This statement inside Main( ) method might look strange for you. During execution,
(param1, param2) => param1 >= param2
Will be interpreted as:
delegate(string param1, string param2){
return param1 >= param2;
}

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 doesn’t contain any parameters? Then the left hand side of lambda operator will contain empty brackets “( )” as shown below:

delegate void sampleDelegate( );
class testLambda
{
public int memberVar = 100;
void memberMethod( ) {
Console.WriteLine(memberVar);
}
static void Main(string[] args)
{
sampleDelegate delObj = ( ) =>memberMethod( );
}
}

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);
class testLambda
{
static void Main(string[] args)
{
System.Linq.Expressions.Expression<sampleDelegate> sampleExp =
param => param * 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;

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.