Lamdba表达式:
using System;
public delegate void TestDelegate1();
public delegate int TestDelegate2(int x, int y);
public delegate int TestDelegate3(int a);
namespace HeroDelegate6
{
class Program
{
//Lambda表达式
//=>Lambda运算符。读作goes to
static void Main(string[] args)
{
TestDelegate1 method1 = delegate ()
{
Console.WriteLine("hello world");
};
//简化:匿名方法的关键字delegate可以省略,在其后加=>
method1 = () => { Console.WriteLine("hello world"); };
TestDelegate2 method2 = (int x, int y) =>
{
return x + y;
};
//简化1:如果Lambda的方法体中只有一个返回值,那么大括号和return可以省略
method2 = (int x, int y) => x + y;
//简化2:在Lambda的参数列表中,参数类型可以省略
method2 = (x, y) => x + y;
//简化3:在Lambda中,如果参数列表中的参数只有一个,那么参数括号可以省略
TestDelegate3 method3 = x => x * x;
}
}
}