In .NET Core, a lambda expression is an anonymous function that can be used to create delegates or expression trees. Lambda expressions are typically used to simplify code and make it more concise, especially when working with collections or performing operations on individual items.
A lambda expression is written as a parameter list, followed by the lambda operator =>, and then the expression or statement block. For example, the following lambda expression represents a method that takes two integers and returns their sum:
(int x, int y) => x + y
Lambda expressions can be used in a variety of contexts in .NET Core, including LINQ queries, event handlers, and functional programming constructs like higher-order functions. Here's an example of using a lambda expression in a LINQ query to filter a list of integers:
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var filteredNumbers = numbers.Where(n => n % 2 == 0);
In this example, the lambda expression (n => n % 2 == 0) represents a method that takes an integer n and returns true if it's even and false otherwise. The Where method uses this lambda expression to filter the list of numbers and return only the even ones.
No comments:
Post a Comment
Please keep your comments relevant.
Comments with external links and adult words will be filtered.