创建一个控制台应用程序实现一个委托

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyDelegate
{
delegate int myDelegataHandler(int a, int b);//声明一个委托
public class A
{
public static int M1(int a,int b)//静态处理方法
{
int c = 0;
c = a + b;
return c;
}
}
public class B
{
static void Main(string[] args)
{
myDelegataHandler mdh = new myDelegataHandler(A.M1);//实现一个委托,简单的理解 方法的委托就是方法的别名,通过委托不但可以执行方法,而且可以将方法传到其他方法中,实现方法回调、
int sum =mdh(2,2);
Console.WriteLine(sum.ToString());
Console.ReadKey();
}
}

}