A lambda expression is an anonymous function
that you can use to create delegates or expression tree types. By using lambda
expressions, you can write local functions that can be passed as arguments or
returned as the value of function calls. Lambda expressions are particularly
helpful for writing LINQ query expressions. Lambda expression you can use in two
case one when you want to make tree business logic and second when you want
simplify code in delegate. In order to understand lambda function, just have
quick look of Delegate in C#.
What is delegate in C#?
A delegate in C# is similar to a function pointer in C or C++. Using a delegate
allows the programmer to encapsulate a reference to a method inside a delegate
object. The delegate object can then be passed to code which can call the
referenced method, without having to know at compile time which method will be
invoked. Unlike function pointers in C or C++, delegates are object-oriented,
type-safe, and secure.
Let's create a simple delegate that will called square function.
Code
using System;
class Example
{
delegate int delPointer(int i);
static int Square(int x)
{
return x * x;
}
static delPointer obj = Square; //Delegate pointing to Square method..
public static void Main()
{
//Invoking deleaget


Join the conversation! Your thoughts help the community grow.