委托和事件 (附例子)
委托
[访问修饰符] delegate 返回类型 委托名 ( );
1 using System;
2
3 namespace Example_3
4 {
5 /// <摘要>
6 /// 该程序演示委托的用法。
7 /// </摘要>
8
9 class Delegates
10 {
11 // 委托定义
12 public delegate int Call(int num1, int num2);
13
14 class Math
15 {
16 // 乘法方法
17 public int Multiply(int num1, int num2)
18 {
19 return num1*num2;
20 }
21
22 // 除法方法
23 public int Divide(int num1, int num2)
24 {
25 return num1/num2;
26 }
27 }
28
29 class TestDelegates
30 {
31 /// <摘要>
32 /// 应用程序的主入口点。
33 /// </摘要>
34 [STAThread]
35 static void Main(string[] args)
36 {
37 // 存储结果的变量
38 int result;
39
40 // 委托的对象
41 Call objCall;
42 // Math 类的对象
43 Math objMath = new Math();
44
45 // 将方法与委托关联起来
46 objCall = new Call(objMath.Multiply);
47
48 // 实例化该委托
49 result = objCall(5, 3);
50 System.Console.WriteLine("结果为 {0}", result);
51 }
52 }
53 }
54 }
2
3 namespace Example_3
4 {
5 /// <摘要>
6 /// 该程序演示委托的用法。
7 /// </摘要>
8
9 class Delegates
10 {
11 // 委托定义
12 public delegate int Call(int num1, int num2);
13
14 class Math
15 {
16 // 乘法方法
17 public int Multiply(int num1, int num2)
18 {
19 return num1*num2;
20 }
21
22 // 除法方法
23 public int Divide(int num1, int num2)
24 {
25 return num1/num2;
26 }
27 }
28
29 class TestDelegates
30 {
31 /// <摘要>
32 /// 应用程序的主入口点。
33 /// </摘要>
34 [STAThread]
35 static void Main(string[] args)
36 {
37 // 存储结果的变量
38 int result;
39
40 // 委托的对象
41 Call objCall;
42 // Math 类的对象
43 Math objMath = new Math();
44
45 // 将方法与委托关联起来
46 objCall = new Call(objMath.Multiply);
47
48 // 实例化该委托
49 result = objCall(5, 3);
50 System.Console.WriteLine("结果为 {0}", result);
51 }
52 }
53 }
54 }
事件
[访问修饰符] event 委托名 事件名;
借助委托定义事件
订阅事件
事件名 += new 委托名(实例化委托)
引发事件
if (cond)
{
事件( ) ;
}
1 using System;
2
3 namespace DelegateExample
4 {
5 class Student
6 {
7 //定义委托
8 public delegate void DelegateRegisterFinish();
9
10 //通过委托定义事件
11 public event DelegateRegisterFinish RegisterFinish;
12
13 private string _name;
14
15 //此例只指定一个带参构造函数
16 public Student(string name)
17 {
18 _name = name;
19 }
20
21 public void Register()
22 {
23 Console.WriteLine("学生{0}进行注册。", _name);
24
25 //事件引发
26 if (RegisterFinish != null)
27 {
28 RegisterFinish();
29 }
30 }
31 }
32 }
2
3 namespace DelegateExample
4 {
5 class Student
6 {
7 //定义委托
8 public delegate void DelegateRegisterFinish();
9
10 //通过委托定义事件
11 public event DelegateRegisterFinish RegisterFinish;
12
13 private string _name;
14
15 //此例只指定一个带参构造函数
16 public Student(string name)
17 {
18 _name = name;
19 }
20
21 public void Register()
22 {
23 Console.WriteLine("学生{0}进行注册。", _name);
24
25 //事件引发
26 if (RegisterFinish != null)
27 {
28 RegisterFinish();
29 }
30 }
31 }
32 }
1 using System;
2
3 namespace DelegateExample
4 {
5 class RegisterStudent
6 {
7
8 static void Main()
9 {
10 Console.Write("输入注册的学生名称:");
11 string studentName = Console.ReadLine();
12
13 //定义一个包含事件的类
14 Student student = new Student(studentName);
15
16 //添加一次订阅 实例化该委托
17 student.RegisterFinish +=
new DelegateExample.Student.DelegateRegisterFinish(student_RegisterFinish);
18
19 //引发注册事件
20 student.Register();
21
22 }
23
24 private static void student_RegisterFinish()
25 {
26 Console.WriteLine("注册成功。");
27 Console.ReadLine();
28 }
29 }
30 }
2
3 namespace DelegateExample
4 {
5 class RegisterStudent
6 {
7
8 static void Main()
9 {
10 Console.Write("输入注册的学生名称:");
11 string studentName = Console.ReadLine();
12
13 //定义一个包含事件的类
14 Student student = new Student(studentName);
15
16 //添加一次订阅 实例化该委托
17 student.RegisterFinish +=
new DelegateExample.Student.DelegateRegisterFinish(student_RegisterFinish);
18
19 //引发注册事件
20 student.Register();
21
22 }
23
24 private static void student_RegisterFinish()
25 {
26 Console.WriteLine("注册成功。");
27 Console.ReadLine();
28 }
29 }
30 }