委托 匿名 lambda表达式

时间:2022-03-09 18:49:21
#region 委托
delegate int ACT(int a, int b);
static void Main(string[] args)
{
ACT act = new ACT(add);
int i = add(5, 8);
Console.WriteLine(i);
Console.ReadKey();
}
public static int add(int a, int b)
{
return a + b;
}
#endregion


#region 匿名
delegate int ACT(int a, int b);
static void Main()
{
ACT act = delegate (int num1, int num2)
{
return num1 + num2;
};
int he = act(4, 8);
Console.WriteLine(he);
Console.ReadKey();
}
#endregion

#region Lambda表达式
delegate int ACT(int a, int b);
static void Main()
{
ACT act = (a, b) => a + b;
int he = act(4, 8);
Console.WriteLine(he);
Console.ReadKey();
}
#endregion