Anatomy of the Lambda Expression
C# 3.0(.NET 3.5) introduced the lambda expression along with LINQ. The lambda expression is a shorter way of representing anonymous methods using some special syntax.
For example, following anonymous method checks if student is teenager or not:
Anonymous method in C#:
delegate(Student s) { return s.Age > 12 && s.Age < 20; };
Anonymous method in VB.Net:
Dim isStudentTeenAger = Function(s As Student) As Boolean
Return s.Age > 12 And s.Age < 20
End Function
The above anonymous method can be represented using a Lambda Expression in C# and VB.Net as below:
Lambda Expression in C#:
s => s.Age > 12 && s.Age < 20
Lambda Expression in VB.Net:
Function(s) s.Age > 12 And s.Age < 20
Let's see how the lambda expression evolved from the following anonymous method.
Anonymous method in C#:
delegate(Student s) { return s.Age > 12 && s.Age < 20; };
The Lambda expression evolves from anonymous method by first removing the delegate keyword and parameter type and adding a lambda operator:
The above lambda expression is absolutely valid, but we don't need the curly braces, return and semicolon if we have only one statement that returns a value. So we can eliminate it.
Also, we can remove parenthesis (), if we have only one parameter.
Thus, we got the lambda expression:
s => s.Age > 12 && s.Age < 20
where s
is a parameter,=>
is the lambda operator and s.Age > 12 && s.Age < 20
is the body expression:
Same way we got lambda expression in VB.Net can be written as below:
Note :VB.Net doesn't support lambda operator
=>
Lambda Expression with Multiple parameters:
You can wrap the parameters in parenthesis if you need to pass more than one parameter, as below:
Example: Specify multiple parameters in lambda expression C#
(s, youngAge) => s.Age >= youngage;
You can also give type of each parameters if parameters are confusing:
Example: Specify parameter type in lambda expression C#
(Student s,int youngAge) => s.Age >= youngage;
Example: Specify multiple parameters in lambda expression VB.Net
Function(s, youngAge) s.Age >= youngAge
Lambd expression without any parameter:
It is not necessary to have atleast one parameter in a lambda expression. The lambda expression can be specify without any parameter also.
Example: Lambda expression with zero parameter.
() => Console.WriteLine("Parameter less lambda expression")
Multiple statements in body expression:
You can wrap expressions in curly braces if you want to have more than one statement in the body:
Example: Lambda expression C#
(s, youngAge) =>
{
Console.WriteLine("Lambda expression with multiple statements in the body");
Return s.Age >= youngAge;
}
Example: Lambda expression VB.Net
Function(s , youngAge)
Console.WriteLine("Lambda expression with multiple statements in the body")
Return s.Age >= youngAge
End Function
Local variable in Lambda Expression body:
You can declare a variable in the expression body to use it anywhere in the expression body, as below:
Example: Lambda expression C#
s =>
{
int youngAge = 18;
Console.WriteLine("Lambda expression with multiple statements in the body");
return s.Age >= youngAge;
}
Example: Lambda expression VB.Net
Function(s)
Dim youngAge As Integer = 18
Console.WriteLine("Lambda expression with multiple statements in the body")
Return s.Age >= youngAge
End Function
You can invoke lambda expression as you invoke delegates. Learn how to invoke lambda expression in the next section.
Points to Remember:
- Lambda Expression is a shorter way of representing anonymous method.
- Lambda Expression syntax:
parameters => body expression
- Lambda Expression can have zero parameter.
- Lambda Expression can have multiple parameters in parenthesis ().
- Lambda Expression can have multiple statements in body expression in curly brackets {}.
Respect and that i have a dandy present: Where To Buy Houses For Renovation home renovation contractors near me
ReplyDelete