A lambda expression:
It is an anonymous Functionalist that you can use to create delegates or expression tree types.To create a lambda expression, you specify input parameters on the left side of the lambda operator =>, and you put the expression or statement block on the other side. For example, the lambda expression x => x * x .
Example as below
using System.Linq.Expressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Expression del myET = x => x * x;
}
}
Program that uses lambda expressions: C
using System;
class Program
{
static void Main()
{
Function Function1 = x => x + 1;
Function Function2 = x => { return x + 1; };
Functiontion Function3 = (int x) => x + 1;
Function Function4 = (int x) => { return x + 1; };
Function Function5 = (x, y) => x * y;
Action Function6 = () => Console.WriteLine();
Function Function7 = delegate(int x) { return x + 1; };
Function Function8 = delegate { return 1 + 1; };
Console.WriteLine(Function1.Invoke(1));
Console.WriteLine(Function2.Invoke(1));
Console.WriteLine(Function3.Invoke(1));
Console.WriteLine(Function4.Invoke(1));
Console.WriteLine(Function5.Invoke(2, 2));
Function6.Invoke();
Console.WriteLine(Function7.Invoke(1));
Console.WriteLine(Function8.Invoke());
}
}
}