Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajesh GamiRank #30 · 41.5K reputationPosted Nov 15, 2022, 8:31 AM EST
Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don’t need to specify the type of the value that you input thus making it more flexible to use.
The ‘=>’ is the lambda operator which is used in all lambda expressions. The Lambda expression is divided into two parts, the left side is the input and the right is the expression.
The Lambda Expressions can be of two types:
Syntax:
Syntax:
Let us take some examples to understand the above concept better.
Example 1: In the code given below, we have a list of integer numbers. The first lambda expression evaluates every element’s square { x => x*x } and the second is used to find which values are divisible by 3 { x => (x % 3) == 0 }. And the foreach loops are used for displaying.
// C# program to illustrate the // Lambda Expression using System; using System.Collections.Generic; using System.Linq; namespace Lambda_Expressions { class Program { static void Main(string[] args) { // List to store numbers List numbers = new List() {36, 71, 12, 15, 29, 18, 27, 17, 9, 34}; // 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 of // each value in the list 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 3 List divBy3 = numbers.FindAll(x => (x % 3) == 0); // foreach loop to display divBy3 Console.Write("Numbers Divisible by 3 : "); foreach(var value in divBy3) { Console.Write("{0} ", value); } Console.WriteLine(); } } }Output:
The list : 36 71 12 15 29 18 27 17 9 34 Squares : 1296 5041 144 225 841 324 729 289 81 1156 Numbers Divisible by 3 : 36 12 15 18 27 9Example 2: Lambda expressions can also be used with user-defined classes. The code given below shows how to sort through a list based on an attribute of the class that the list is defined upon.
// C# program to illustrate the // Lambda Expression using System; using System.Collections.Generic; using System.Linq; // User defined class Student class Student { // properties rollNo and name public int rollNo { get; set; } public string name { get; set; } } class GFG { // Main Method static void Main(string[] args) { // List with eah element of type Student List details = new List() { new Student{ rollNo = 1, name = "Liza" }, new Student{ rollNo = 2, name = "Stewart" }, new Student{ rollNo = 3, name = "Tina" }, new Student{ rollNo = 4, name = "Stefani" }, new Student { rollNo = 5, name = "Trish" } }; // To sort the details list // based on name of student // in ascending order var newDetails = details.OrderBy(x => x.name); foreach(var value in newDetails) { Console.WriteLine(value.rollNo + " " + value.name); } } }Output:
1 Liza 4 Stefani 2 Stewart 3 Tina 5 TrishRef. https://www.geeksforgeeks.org/lambda-expressions-in-c-sharp/
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions
https://www.tutorialsteacher.com/linq/linq-lambda-expression
NitinRank #140 · 16.1K reputationPosted Nov 14, 2022, 12:45 AM EST
Lambda expression that has an expression as its body:
(input-parameters) => expression
Any lambda expression can be converted to a delegate type. The delegate type to which a lambda expression can be converted is defined by the types of its parameters and return value. If a lambda expression doesn't return a value, it can be converted to one of the delegate. A lambda expression that has one parameter and returns a value can be converted to a Func delegate. In the following example, the lambda expression Y
Actiondelegate types; otherwise, it can be converted to one of theFuncdelegate types. For example, a lambda expression that has two parameters and returns no value can be converted to an Action=> Y * Y, which specifies a parameter that's namedxand returns the value of Y squared, is assigned to a variable of a delegate type:Func square = Y => Y * Y;
Console.WriteLine(square(5));
Output: 25
Expression lambdas can also be converted to the expression tree types, as the following example shows:
System.Linq.Expressions.Expression> e = Y => Y * Y;
Console.WriteLine(e);
Output: Y => (Y * Y)
You can use lambda expressions in any code that requires instances of delegate types or expression trees, for example as an argument to the Task.Run(Action)method to pass the code that should be executed in the background. You can also use lambda expressions when you write LINQ in C#, as the following example shows:
int[] numbers = { 2, 3, 4, 5 };
var squaredNumbers = numbers.Select(Y => Y * Y);
Console.WriteLine(string.Join(" ", squaredNumbers));
Output: 4 9 16 25
Sachin SinghRank #66 · 26.2K reputationPosted Nov 1, 2022, 3:33 AM EDT
please follow my article on this
https://www.sharpencode.com/article/Linq/an-overview-of-linq/lambda-expression-func-predicate-and-action
Vishal YelveRank #178 · 13.6K reputationPosted Nov 1, 2022, 3:47 AM EDT
https://www.c-sharpcorner.com/UploadFile/bd6c67/lambda-expressions-in-C-Sharp/
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Nov 2, 2022, 6:01 AM EDT
https://www.c-sharpcorner.com/UploadFile/ajyadav123/lambda-expression/
https://www.c-sharpcorner.com/UploadFile/dacca2/lambda-expression-in-15-minutes/
https://www.c-sharpcorner.com/blogs/lambda-expression-in-c-sharp
https://www.c-sharpcorner.com/blogs/lambda-expression-in-c-sharp1
Onkar SharmaRank #104 · 20.6K reputationPosted Nov 13, 2022, 10:10 AM EST
Hi Naresh,
To know more about Lambda Expressions in C#, visit:
Thanks!
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 1, 2022, 10:46 AM EDT
https://www.geeksforgeeks.org/lambda-expressions-in-c-sharp/