Anatomy Of The Lambda Expression - TutorialsTeacher
Maybe your like
C# 3.0(.NET 3.5) introduced the lambda expression along with LINQ. The lambda expression is a shorter way of representing anonymous method using some special syntax.
For example, following anonymous method checks if student is teenager or not:
Example: Anonymous Method in C# Copydelegate(Student s) { return s.Age > 12 && s.Age < 20; };Try itExample: Anonymous method in VB.Net CopyDim isStudentTeenAger = Function(s As Student) As Boolean Return s.Age > 12 And s.Age < 20 End FunctionTry itThe above anonymous method can be represented using a Lambda Expression in C# and VB.Net as below:
Example: Lambda Expression in C# Copys => s.Age > 12 && s.Age < 20Try itExample: Lambda Expression in VB.Net CopyFunction(s) s.Age > 12 And s.Age < 20Try itLet's see how the lambda expression evolved from the following anonymous method.
Example: Anonymous method in C# Copydelegate(Student s) { return s.Age > 12 && s.Age < 20; };Try itThe 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:

The lambda expression can be invoked same way as delegate using ().
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# Copy(s, youngAge) => s.Age >= youngAge;Try itYou can also give type of each parameters if parameters are confusing:
Example: Specify Parameter Type Copy<b>(Student s,int youngAge)</b> => s.Age >= youngage;Try itExample: Specify Multiple Parameters in Lambda Expression VB.Net CopyFunction(s, youngAge) s.Age >= youngAgeLambda Expression without 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 without Parameter Copy<b>()</b> => Console.WriteLine("Parameter less lambda expression")Try itMultiple Statements in Lambda Expression Body
You can wrap expressions in curly braces if you want to have more than one statement in the body:
Example: Multi Statements Lambda expression C# Copy(s, youngAge) => <b>{</b> Console.WriteLine("Lambda expression with multiple statements in the body"); Return s.Age >= youngAge; <b>}</b>Try itExample: Multi Statements Lambda Expression VB.Net CopyFunction(s , youngAge) Console.WriteLine("Lambda expression with multiple statements in the body") Return s.Age >= youngAge End FunctionTry itDeclare 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: Local Variable in Lambda expression C# Copys => { int youngAge = 18; Console.WriteLine("Lambda expression with multiple statements in the body"); return s.Age >= youngAge; }Try itExample: Local Variable in Lambda Expression VB.Net CopyFunction(s) Dim youngAge As Integer = 18 Console.WriteLine("Lambda expression with multiple statements in the body") Return s.Age >= youngAge End FunctionTry itLambda expression can also be assigned to built-in delegates such as Func, Action and Predicate.
Assign Lambda Expression to Delegate
The lambda expression can be assigned to Func<in T, out TResult> type delegate. The last parameter type in a Func delegate is the return type and rest are input parameters. Visit Func delegate section of C# tutorials to know more about it.
Consider the following lambda expression to find out whether a student is a teenager or not.
Example: Lambda Expression Assigned to Func Delegate C# CopyFunc<Student, bool> isStudentTeenAger = s => s.age > 12 && s.age < 20; Student std = new Student() { age = 21 }; bool isTeen = isStudentTeenAger(std);// returns falseTry itExample: Lamda Expression Assigned to Func Delegate VB.Net CopyDim isStudentTeenAger As Func(Of Student, Boolean) = Function(s) s.Age > 12 And s.Age < 20 Dim stud As New Student With {.Age = 21} Dim isTeen As Boolean = isStudentTeenAger(stud) // returns falseTry itIn the above example, the Func delegate expects the first input parameter to be of Student type and the return type to be boolean. The lambda expression s => s.age > 12 && s.age < 20 satisfies the Func<Student, bool> delegate requirement, as shown below:

The Func<> delegate shown above, would turn out to be a function as shown below.
Example: Func Delegate as Function Copybool isStudentTeenAger(Student s) { return s.Age > 12 && s.Age < 20; }Action Delegate
Unlike the Func delegate, an Action delegate can only have input parameters. Use the Action delegate type when you don't need to return any value from lambda expression.
Example: Lamda Expression Assigned to Action Delegate C# CopyAction<Student> PrintStudentDetail = s => Console.WriteLine("Name: {0}, Age: {1} ", s.StudentName, s.Age); Student std = new Student(){ StudentName = "Bill", Age=21}; PrintStudentDetail(std);//output: Name: Bill, Age: 21Try itExample: Lamda Expression Assigned to Action Delegate VB.Net CopyDim printStudentDetail As Action(Of Student) = Sub(s) Console.WriteLine("Name: {0}, Age: {1} ", s.StudentName, s.Age) Dim stud As New Student With {.StudentName = "Bill", .Age = 21} printStudentDetail(stud)//output: Name: Bill, Age: 21Try itLambda Expression in LINQ Query
Usually lambda expression is used with LINQ query. Enumerable static class includes Where extension method for IEnumerable<T> that accepts Func<TSource,bool>. So, the Where() extension method for IEnumerable<Student> collection is required to pass Func<Student,bool>, as shown below:

So now, you can pass the lambda expression assigned to the Func delegate to the Where() extension method in the method syntax as shown below:
Example: Func Delegate in LINQ Method Syntax CopyIList<Student> studentList = new List<Student>(){...}; Func<Student, bool> isStudentTeenAger = s => s.age > 12 && s.age < 20; var teenStudents = studentList.Where(isStudentTeenAger).ToList<Student>();Try itExample: Func Delegate in LINQ Query Syntax CopyIList<Student> studentList = new List<Student>(){...}; Func<Student, bool> isStudentTeenAger = s => s.age > 12 && s.age < 20; var teenStudents = from s in studentList where isStudentTeenAger(s) select s;Try itYou can follow the same method in VB.Net to pass Func delegate.
Tag » What Are Lambda Expressions C#
-
Lambda Expressions In C# - GeeksforGeeks
-
Lambda Expressions - C# Reference | Microsoft Docs
-
Understanding Lambda Expressions In C# | Endjin
-
Lambda Expressions In C#
-
Dixin's Blog - Understanding C# Features (5) Lambda Expression ...
-
Lambda Expressions - Using C# LINQ - A Practical Overview
-
How To Use Lambda Expressions In C#
-
Lambda Expressions In C# - Code Maze
-
Lambda Expression In C#— A Quick Guide | By Petey | The Startup
-
C# Lambda Expressions Simplified | Syntax & Practical Examples 101
-
Understand Lambda Expressions In 3 Minutes - CodeProject
-
How To Use Lambda Expressions In C# | InfoWorld
-
LINQ - Lambda Expressions - Tutorialspoint
-
C# Lambda Expressions: Why Should I Use Them? - Stack Overflow