1
/**/
/*
2 * C#的一个自定义的事件
3 */
4 using System;
5 namespace DefaultNamespace
6 {
7 class DelegateClass
8 {
9 public delegate bool Comparator(int one,int two);//声明代理
10 public event Comparator onComparator;//定义事件
11 public void StartComparator()//事件触发方法,只要调用此方法,就会触发所有注册的事件
12 {
13 onComparator(3,4);
14 }
15 }
16
17 class DelegateDemo
18 {
19 //??????
20 public bool SortAsceding(int one,int two)
21 {
22 //return one>two;
23 Console.WriteLine("SortAsceding:"+one+","+two);
24 return true;
25 }
26 //??????
27 public bool SortDescending(int one,int two)
28 {
29 //return two>one;
30 Console.WriteLine("SortDescending"+one+","+two);
31 return true;
32 }
33
34 public static void Main(string[] args)
35 {
36 //代理类对象
37 DelegateClass dc=new DelegateClass();
38 //客户类对象
39 DelegateDemo dd=new DelegateDemo();
40 //注册事件
41 dc.onComparator+=new DelegateClass.Comparator(dd.SortAsceding);
42 dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
43 dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
44 dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
45 //事件触发
46 dc.StartComparator();
47
48 }
49 }
50}
51
2 * C#的一个自定义的事件
3 */
4 using System;
5 namespace DefaultNamespace
6 {
7 class DelegateClass
8 {
9 public delegate bool Comparator(int one,int two);//声明代理
10 public event Comparator onComparator;//定义事件
11 public void StartComparator()//事件触发方法,只要调用此方法,就会触发所有注册的事件
12 {
13 onComparator(3,4);
14 }
15 }
16
17 class DelegateDemo
18 {
19 //??????
20 public bool SortAsceding(int one,int two)
21 {
22 //return one>two;
23 Console.WriteLine("SortAsceding:"+one+","+two);
24 return true;
25 }
26 //??????
27 public bool SortDescending(int one,int two)
28 {
29 //return two>one;
30 Console.WriteLine("SortDescending"+one+","+two);
31 return true;
32 }
33
34 public static void Main(string[] args)
35 {
36 //代理类对象
37 DelegateClass dc=new DelegateClass();
38 //客户类对象
39 DelegateDemo dd=new DelegateDemo();
40 //注册事件
41 dc.onComparator+=new DelegateClass.Comparator(dd.SortAsceding);
42 dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
43 dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
44 dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
45 //事件触发
46 dc.StartComparator();
47
48 }
49 }
50}
51