Lambda expression in C#
Loading
Lambda expression in C#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajesh GamiPosted Nov 15, 2022, 1:31 PM
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:
input => { statements };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.
Output:
Example 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.
Output:
Ref. 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
NitinPosted Nov 14, 2022, 5:45 AM
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
Onkar SharmaPosted Nov 13, 2022, 3:10 PM
Hi Naresh,
To know more about Lambda Expressions in C#, visit:
Thanks!
Rajanikant HawaldarPosted Nov 2, 2022, 10:01 AM
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
Brahma Prakash ShuklaPosted Nov 1, 2022, 2:46 PM
https://www.geeksforgeeks.org/lambda-expressions-in-c-sharp/
Vishal YelvePosted Nov 1, 2022, 7:47 AM
https://www.c-sharpcorner.com/UploadFile/bd6c67/lambda-expressions-in-C-Sharp/
Sachin SinghPosted Nov 1, 2022, 7:33 AM
please follow my article on this
https://www.sharpencode.com/article/Linq/an-overview-of-linq/lambda-expression-func-predicate-and-action