C#中处定义事件

 

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
 2InBlock.gif * C#的一个自定义的事件
 3ExpandedBlockEnd.gif */

 4 None.gif using  System;
 5 None.gif namespace  DefaultNamespace
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    class DelegateClass
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public delegate bool Comparator(int one,int two);//声明代理
10InBlock.gif        public event Comparator onComparator;//定义事件
11InBlock.gif        public void StartComparator()//事件触发方法,只要调用此方法,就会触发所有注册的事件
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            onComparator(3,4);
14ExpandedSubBlockEnd.gif        }

15ExpandedSubBlockEnd.gif    }

16InBlock.gif    
17InBlock.gif    class DelegateDemo
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        //??????
20InBlock.gif        public bool SortAsceding(int one,int two)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            //return one>two;
23InBlock.gif            Console.WriteLine("SortAsceding:"+one+","+two);
24InBlock.gif            return true;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif        //??????
27InBlock.gif        public bool SortDescending(int one,int two)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            //return two>one;
30InBlock.gif            Console.WriteLine("SortDescending"+one+","+two);
31InBlock.gif            return true;
32ExpandedSubBlockEnd.gif        }

33InBlock.gif        
34InBlock.gif        public static void Main(string[] args)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            //代理类对象
37InBlock.gif            DelegateClass dc=new DelegateClass();
38InBlock.gif            //客户类对象
39InBlock.gif            DelegateDemo dd=new DelegateDemo();
40InBlock.gif            //注册事件
41InBlock.gif            dc.onComparator+=new DelegateClass.Comparator(dd.SortAsceding);
42InBlock.gif            dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
43InBlock.gif            dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
44InBlock.gif            dc.onComparator+=new DelegateClass.Comparator(dd.SortDescending);
45InBlock.gif            //事件触发
46InBlock.gif            dc.StartComparator();
47InBlock.gif            
48ExpandedSubBlockEnd.gif        }

49ExpandedSubBlockEnd.gif    }

50ExpandedBlockEnd.gif}
    
51 None.gif

转载于:https://www.cnblogs.com/sangrei/archive/2007/06/05/771561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值