本例子是一個關於委托的小例子[貓叫,狗跳,人喊]。
委托是C#開發中一個非常重要的概念,理解起來也和常規的方法不同,但一旦理解清楚,就可以信手拈來,隨處可用。
委托是對方法的抽象。它存儲的就是一系列具有相同簽名和返回回類型的方法的地址。調用委托的時候,委托包含的所有方法將被執行。
涉及到的知識點:
委托的定義
----------------------------------------------------------------------------------
效果圖如下【點擊打開貓,彈出CatForm,點擊喵喵按鈕,觸發事件,調用其他類的委托】:

------------------------------------------------------------------------------------------------
代碼如下:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace DemoDelegate
7 {
8 public delegate void Handler();//定義一個委托類型
9 /// <summary>
10 /// 貓貓
11 /// </summary>
12 public class Cat
13 {
14
15 public Handler Miao;
16
17 /// <summary>
18 /// Miao叫的動作
19 /// </summary>
20 public void MaioMiao()
21 {
22 var action = Miao;
23 if (action != null)
24 {
25 action();
26 }
27 }
28 }
29 }
30 ///////////////////////////////////////////////////
31 using System;
32 using System.Collections.Generic;
33 using System.Linq;
34 using System.Text;
35
36 namespace DemoDelegate
37 {
38 /// <summary>
39 /// 可愛的狗狗
40 /// </summary>
41 public class Dog
42 {
43 public Handler Tiao;
44
45 /// <summary>
46 /// 狗跳的動作
47 /// </summary>
48 public void DogTiao()
49 {
50 var action = Tiao;
51 if (action != null)
52 {
53 action();
54 }
55 }
56 }
57 }
58
59 ///////////////////////////////////////////////
60
61 using System;
62 using System.Collections.Generic;
63 using System.Linq;
64 using System.Text;
65
66 namespace DemoDelegate
67 {
68 /// <summary>
69 /// 主人
70 /// </summary>
71 public class Master
72 {
73 public Handler Han;
74
75 public void HanJiao()
76 {
77 var action = Han;
78 if (action != null)
79 {
80 action();
81 }
82 }
83
84 }
85 }
View Code
界面類代碼如下:

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace DemoDelegate
11 {
12 public partial class MainForm : Form
13 {
14 private Dog dog;
15
16 private Master master;
17
18 public MainForm()
19 {
20 InitializeComponent();
21 InitInfo();
22 }
23
24 /// <summary>
25 /// 初始化信息
26 /// </summary>
27 private void InitInfo() {
28 dog = new Dog();
29 dog.Tiao += TiaoMethod;
30 master = new Master();
31 master.Han += HanMethod;
32 }
33
34 /// <summary>
35 /// 喵喵
36 /// </summary>
37 /// <param name="sender"></param>
38 /// <param name="e"></param>
39 private void btnCatMiao_Click(object sender, EventArgs e)
40 {
41 CatForm catForm = new CatForm();
42 catForm.MiaoAction += MaioMethod;
43 catForm.ShowDialog();
44 }
45
46 private void MaioMethod(object sender,EventArgs e) {
47 this.txtCat.Text = "貓在父頁面叫了";
48 this.dog.DogTiao();
49 this.master.HanJiao();
50 }
51
52 private void TiaoMethod()
53 {
54 this.txtDog.Text = "狗跳了";
55 }
56
57 private void HanMethod()
58 {
59 this.txtMaster.Text = "別叫了";
60 }
61 }
62 }
63 /////////////////////////////////////////////////////////////////
64 using System;
65 using System.Collections.Generic;
66 using System.ComponentModel;
67 using System.Data;
68 using System.Drawing;
69 using System.Linq;
70 using System.Text;
71 using System.Windows.Forms;
72
73 namespace DemoDelegate
74 {
75 public partial class CatForm : Form
76 {
77 private Cat cat;
78
79 public event EventHandler MiaoAction;
80
81 public CatForm()
82 {
83 InitializeComponent();
84 InitInfo();
85 }
86
87 public void InitInfo() {
88 cat = new Cat();
89 cat.Miao += MaioMethod;
90 }
91
92 private void MaioMethod()
93 {
94 this.txtCat.Text = "貓叫了";
95 var action = MiaoAction;
96 if (action != null) {
97 action(cat, null);
98 }
99 }
100
101 private void btnCatMiao_Click(object sender, EventArgs e)
102 {
103 this.cat.MaioMiao();
104 }
105 }
106 }
View Code
關於委托,功能和用途還有很多,這裡只是拋磚引玉,希望能對大家有幫助