委托是C#中比较复杂的概念,C#中的委托和C/C++中的函数指针非常相似,使用委托可以把委托内部方法的引用封装起来然后通过它使用委托引用的方法。它有一个特性:不需要知道被引用的方法属于哪一个类对象,只要函数的参数个数与返回类型与委托对象一致就可以使用。这样说可能比较抽象,下面举几个简单的例子希望能给广大初学C#的朋友一些基本的认识
//定义一个返回值为string的无参数的委托注意这个委托只能引用对象中返回值为string的无参数方法
1delegate string MyDelegate();
2public class MyClass
3{
4 public string SayHello()
5 {
6 return "Hello the world!";
7 }
8
9}
10public class TestMyClass
11{
12 public static void Main(string[] args)
13 {
14 MyClass myClass1=new MyClass();
15 MyDelegate myDelegate1=new MyDelegate(myClass1.SayHello);
16
17 //下面就使用myDelegate1代替对象myClass1的SayHello方法
18 System.Console.WriteLine(myDelegate1());
19
20 //输出结果为hello the world! 与调用myClass1.SayHello();效果相同
21 }
22}
23
2public class MyClass
3{
4 public string SayHello()
5 {
6 return "Hello the world!";
7 }
8
9}
10public class TestMyClass
11{
12 public static void Main(string[] args)
13 {
14 MyClass myClass1=new MyClass();
15 MyDelegate myDelegate1=new MyDelegate(myClass1.SayHello);
16
17 //下面就使用myDelegate1代替对象myClass1的SayHello方法
18 System.Console.WriteLine(myDelegate1());
19
20 //输出结果为hello the world! 与调用myClass1.SayHello();效果相同
21 }
22}
23
如果委托只有这点功能它就没有什么太大的用处了,委托还有一个非常有用的功能:定义复合委托对象。并且只有同样类型的委托才能够复合起来
形成一个委托链:
+: 能添加一个委托对象到委托链中对
-:能在委托链中去掉一个委托对象
1delegate void MyDelegate(string s);
2public class MyClass
3{
4 public void SayHello(string who)
5 {
6 System.Console.WriteLine( who+"hello!");
7 }
8 public void SayGoodBye(string who)
9 {
10 {
11 public static void Main(string[] args)
12 {
13 MyClass myClass1=new MyClass();
14 MyDelegate myDelegate1=new MyDelegate(myClass1.SayHello);
15
16
17 //下面就使用myDelegate1代替对象myClass1的SayHello方法
18 System.Console.WriteLine(myDelegate1());
19
20
21 //输出结果为hello the world! 与调用myClass1.SayHello();效果相同
22 }
23 }
24}
25}
26
2public class MyClass
3{
4 public void SayHello(string who)
5 {
6 System.Console.WriteLine( who+"hello!");
7 }
8 public void SayGoodBye(string who)
9 {
10 {
11 public static void Main(string[] args)
12 {
13 MyClass myClass1=new MyClass();
14 MyDelegate myDelegate1=new MyDelegate(myClass1.SayHello);
15
16
17 //下面就使用myDelegate1代替对象myClass1的SayHello方法
18 System.Console.WriteLine(myDelegate1());
19
20
21 //输出结果为hello the world! 与调用myClass1.SayHello();效果相同
22 }
23 }
24}
25}
26
事件驱动是windows应用程序的重要特征,C#委托就是用于产生事件,事件就是用于在一个组件中监听这个组件的变化
下面再举一个简单的例子:
1//定义一个事件代理(委托)
2public delegate void EventHandler(string str);
3//定义事件源类
4class EventSource
5{
6 //定义委托作为事件源类的成员
7 public event EventHandler Say;
8 public void TriggerEvent()
9 {
10 if(this.Say!=null)
11
12
13 //因为Say是个委托所以执行Say方法所做的实际操作由注册到它的事件处理函数决定
14Say("A event take place!");
15}
16}
17
18
19//测试
20class Test
21{
22 public static void Main()
23 {
24 EventSource aEventSource=new EventSource();
25
26
27 //注册事件处理函数为MyEvent 显示一串字符类似于this.Click+=new
28 EventHandler(Button1_OnClick);
29
30
31 aEventSource.Say+=new EventHandler(MyEvent);
32
33
34 //此处为演示事件触发过程所以就用程序自动触发
35 //在图形界面应用程序中,一般由用户触发事件,后由操作系统发送
36 //消息并调用处理函数,所以程序员只要注册事件处理函数
37 //和编写事件处理函数的代码其他就不用关心了
38 aEventSource.TriggerEvent();
39}
40 //事件处理函数
41 public static void MyEvent(string str)
42 {
43 System.Console.WriteLine(str);
44 }
45}
2public delegate void EventHandler(string str);
3//定义事件源类
4class EventSource
5{
6 //定义委托作为事件源类的成员
7 public event EventHandler Say;
8 public void TriggerEvent()
9 {
10 if(this.Say!=null)
11
12
13 //因为Say是个委托所以执行Say方法所做的实际操作由注册到它的事件处理函数决定
14Say("A event take place!");
15}
16}
17
18
19//测试
20class Test
21{
22 public static void Main()
23 {
24 EventSource aEventSource=new EventSource();
25
26
27 //注册事件处理函数为MyEvent 显示一串字符类似于this.Click+=new
28 EventHandler(Button1_OnClick);
29
30
31 aEventSource.Say+=new EventHandler(MyEvent);
32
33
34 //此处为演示事件触发过程所以就用程序自动触发
35 //在图形界面应用程序中,一般由用户触发事件,后由操作系统发送
36 //消息并调用处理函数,所以程序员只要注册事件处理函数
37 //和编写事件处理函数的代码其他就不用关心了
38 aEventSource.TriggerEvent();
39}
40 //事件处理函数
41 public static void MyEvent(string str)
42 {
43 System.Console.WriteLine(str);
44 }
45}