A lambda expression is an anonymous function means no name of method. The => token is called the lambda operator. It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side.
Example
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = x => x * x;
int my = myDelegate(5); //my = 25
}
Example 2:
public static void Main()
{
Func<int, int> t = x => x + 1;
Func<int, int> t2 = x => { return x + 1; };
Func<string, string> t3 = x => { return x; };
Action<int> example1 = (int x) => Console.WriteLine("{0}", x);
Console.WriteLine(t.Invoke(1));
example1.Invoke(7);
Console.ReadLine();
}
Output : 2
7
Example
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = x => x * x;
int my = myDelegate(5); //my = 25
}
Example 2:
public static void Main()
{
Func<int, int> t = x => x + 1;
Func<int, int> t2 = x => { return x + 1; };
Func<string, string> t3 = x => { return x; };
Action<int> example1 = (int x) => Console.WriteLine("{0}", x);
Console.WriteLine(t.Invoke(1));
example1.Invoke(7);
Console.ReadLine();
}
Output : 2
7