In my earlier article we have seen the following new features of C# 3.0.
- Extension Methods
- Automatic Properties
- Implicitly Typed variables
- Object Initializers and Collection Initializers
- Anonymous Types
In this blog we will try to understand the concept of Lambda expressions.
What is Lambda Expression?
Lambda expressions are nothing but anonymous functions. Lambda expressions can be used to write expressions or statements. To write Lambda expression one has to make use of the Lambda operator which is read as “goes to”. Lambda operator is represented as “=>” i.e. an equal to sign followed by a greater than symbol. Lambda expressions normally have input parameters on the left side of the Lambda operator and expressions/statements on the right side. The lambda operator is used to separate the input arguments on the left side from the expression/statements on the right side. Lambda expressions are extensively used in LINQ. An e.g. of Lambda expression is given below.
public class LambdaExpressionDemo public double CountEmp() |
In the above code one can see we have declared a delegate which returns an integer value and takes two integers as input parameters. Also you can see I have declared a delegate variable (addResult) and assigned an anonymous function using our Lambda expression. One thing to note here is that there is no need to declare the type of the input arguments for the anonymous functions. The compiler will infer the type. What the above lambda expressions means is that we have an anonymous function with two input parameters (left side of the Lambda operators are input parameters) which needs to be added and the result returned. Don’t you thing this is a very simple way of declaring a function. In the second e.g. I have made use of Lambda expression along with Count method. The lambda expression evaluates and checks whether the person’ LName starts with “la” and returns true or false. If it returns true then that record is counted. So the count will be 2.
To better understand the advantages/usefullness of Lambda expression we need to know how we use to implement these things in the earlier versions of C#. Lets see how we would do this in C# 2.0 using anonymous methods.
delegate int Add(int a, int b); |
In the above e.g. we are making use of Anonymous method feature of C# 2.0 where we are making use of delegates to declare an Anonymous methods (highlighted in green). Wherever you thing creating a method is redundant or will create overhead you can go ahead and make use of Anonymous methods. Also one can use anonymous methods in cases where a method or property or constructor expects a delegate. In the above code we have declared a delegate with the signature of the method and then again using delegate variable, addResult, we are declaring an Anonymous method. One can write any number of statements inside Anonymous methods. The scope of the variables declared in Anonymous methods are within the limits of Anonymous methods.
Now lets see how we would have done this in the good olden days of C#1.0.
delegate int Add(int a, int b); public int Addition(int a, int b) |
In the above code you can see the good olden days of delegate declaration where one has to create a method (Addition) and then create a delegate (Add) representing the method. Once you have declared the delegate one can assign the method. So after seeing the olden ways of declaring the same functionality which can be achieved using Lambda expression one can see the advantages.
One can define two types of lambda, an Expression Lambda and a Statement Lambda. Lets see what each one of it means.
Expression Lambda: Expression lambda is nothing but an expression on the right side of the Lambda Operator. E.g are given below.
(a, b) => (a + b); (int a, int b) => a > b; |
An expressions is any piece of code which can be evaluated to a single value, object etc. So the above code are just e.g. of expressions, even an assignment of a value to a variable is also an expression.
Statement Lambdas: As the name suggests statement lambdas are ones which can have more than one line of code/statement. In expression lambda you normally have only one line of statement which executes and produces the output in the form of a single value or object etc whereas in statement lambda we have more than one line of statements enclosed in a curly brace ({}).
delegate int Add(int a, int b); |
In the above code you can see we have written some three lines of code and this is an e.g. of statement Lambda. There are no restriction to the number of lines of code that can be written in a Lambda expression. As a good practice it will be nice if you can restrict the number of lines between 2 and 3 lines.
Some points about Lambda expressions
- Lambda expressions are anonymous functions.
- No need to declare the types of the variables used in the input parameter of the Lambda expression. The types will be inferred by the compiler.
- If there are situation when the compiler cannot infer the type of the variable used, in those kind of scenarios we can explicitly declare the types for the input parameters as shown below.
delegate int Add(int a, int b); |
- Variables declared in a Lambda expressions are scoped inside the Lambda blocks.
- Lambda declaration should match the signature of the delegate i.e. the number of parameters and return type.
- Parameter less Lambdas can also be declared just by having a empty parentheses as shown below.
delegate void Print(); |
- You can pass Lambda expression where a delegate is expected. E.g. is shown below.
/*Lambda expression passed as an argument where delegate was expected.*/ |
- When you are making use of Lambda expressions behind the scene the compiler does the extra work of creating an anonymous function for the Lambda expression. Lets see with an e.g.: the code and generated IL are pasted below.
/*Class having a delegate and a Lambda expression.*/ /*Below is the MSIL generated for the above method. */ /*C# code decompiled from MSIL using .NET Reflector*/ // Nested Types |
In the above pasted code you can see the MSIL generated is making use of AnonymousMethodDelegate i.e. the compiler is converting Lambda expressions into Anonymous functions. This is proven from the decompiled C# code from the MSIL pasted above.
As with other features of C# 3.0 Lambda expression is also a way of writing terse code where the compiler does the extra work of generating the extra codes. In my next we will have a look at Language Integrated Query (LINQ) for which all these features were incorporated in C#. Till then write terse code and try to learn more.
Sandeep
I wrote a similar article describing lambda expressions for beginners.
ReplyDeleteTrade FX At Home On Your PC: tradeatf Is A Forex Trading Company. The Company States That You Can Make On Average 80 – 300 Pips Per Trade. tradeatf States That It Is Simple And Easy To Get Started.
ReplyDeleteThis article dives deep into lambda expressions in C# 3.0, highlighting their powerful features and practical applications—essential for modern programming! If you're also a gamer, Sky node is a fantastic tool to help you manage your game library while you enhance your C# skills.
ReplyDelete