Lambda expressions in C# are used to perform anonymous functions. And the difference is that to insert a lambda expression you do not need to specify the type of the value and hence it is easier to use.
The declaration operator '=>' is a Lambda function used to separate a lambda's parameter list from its body. The lambda expression is divided into two parts. On the left is the input and on the right is the expression.
It includes input and expression as its body.
SyntaxFollowing is the syntax of the expression lambda.
csharpInput-parameters => expression;
It consists of the input and the set of statement blocks as their body, which must be executed.
SyntaxFollowing is the syntax of the statement lambda.
csharpInput-parameters => {};
To create a lambda expression, you specify the input parameters (if any) to the left of the lambda operator, and an expression or statement block on the other side.
A lambda expression can be converted to a delegate type. The types of its parameters and return value define the delegate type to which a lambda expression can be converted. If a lambda expression doesn't return a value, it can be converted to one of the Action delegate types; otherwise, it can be converted to one of the Func delegate types.
Let’s understand the above concept with the following example:
In the following example, we have two lambda expressions and a list of integers; The first lambda expression is used to calculate the square of each element and the second lambda expression finds values divisible by 2.
csharpusing System; using System.Collections.Generic; using System.Linq; namespace Lambda_Expressions { class Program { static void Main(string[] args) { // List to store numbers List<int> numbers = new List<int>() { 10, 5, 20, 15, 7, 8 }; // foreach loop to display the list Console.Write("The list : "); foreach(var value in numbers) { Console.Write("{0} ", value); } Console.WriteLine(); // Using lambda expression // to calculate square var square = numbers.Select(x => x * x); // foreach loop to display squares Console.Write("Squares : "); foreach(var value in square) { Console.Write("{0} ", value); } Console.WriteLine(); // Using Lambda expression to // find all numbers in the list // divisible by 2 List<int> divBy3 = numbers.FindAll(x => (x % 2) == 0); // foreach loop to display divBy3 Console.Write("Numbers Divisible by 2 : "); foreach(var value in divBy3) { Console.Write("{0} ", value); } Console.WriteLine(); } } }
Output
Example
Lambda expressions can also be used with user classes. The code below shows how to sort the list based on the type of classes in which the list is defined.
csharpusing System; using System.Collections.Generic; using System.Linq; class Student { // properties rollNo and name public int rollNo { get; set; } public string name { get; set; } } class Details { static void Main(string[] args) { // List with each element of type Student List<Student> details = new List<Student>() { new Student{ rollNo = 1, name = "Liza" }, new Student{ rollNo = 2, name = "Tina" }, new Student{ rollNo = 3, name = "Mina" }, new Student{ rollNo = 4, name = "Deeka" }, new Student { rollNo = 5, name = "Sikka" } }; // To sort the details list // based on name of student var newDetails = details.OrderBy(x => x.name); foreach(var value in newDetails) { Console.WriteLine(value.rollNo + " " + value.name); } } }
Output
Copyrights © 2024 letsupdateskills All rights reserved