对C#委托、事件、自定义事件的理解



一、委托
委托类似于函数指针,但函数指针只能引用静态方法,而委托既能引用静态方法,也能引用实例方法。

委托使用分三步:1、委托声明。2、委托实例化。3、委托调用。
例程一:

程序代码

 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  委托
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gifdelegate int NumOpe(int a,int b); //委托声明
 6InBlock.gifclass Class1
 7ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 8InBlock.gifstatic void Main(string[] args)
 9ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
10InBlock.gifClass1 c1 = new Class1();
11InBlock.gifNumOpe p1 = new NumOpe(c1.Add); //委托实例化
12InBlock.gifConsole.WriteLine(p1(1,2)); //委托调用
13InBlock.gifConsole.ReadLine();
14ExpandedSubBlockEnd.gif}

15InBlock.gif
16InBlock.gifprivate int Add(int num1,int num2)
17ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
18InBlock.gifreturn(num1+num2);
19ExpandedSubBlockEnd.gif}

20ExpandedSubBlockEnd.gif}

21ExpandedBlockEnd.gif}

22 None.gif
23 None.gif

例中,委托NumOpe引用了方法Add。
委托声明了以后,就可以象类一样进行实例化,实例化时把要引用的方法(如:Add)做为参数,这样委托和方法就关联了起来,就可以用委托来引用方法了。
委托和所引用的方法必须保持一致:
1、参数个数、类型、顺序必须完全一致。
2、返回值必须一致。

二、事件

事件有很多,比如说鼠标的事件:MouserMove,MouserDown等,键盘的事件:KeyUp,KeyDown,KeyPress。

有事件,就会有对事件进行处理的方法,而事件和处理方法之间是怎么联系起来的呢?委托就是他们中间的桥梁,事件发生时,委托会知道,然后将事件传递给处理方法,处理方法进行相应处理。

比如在WinForm中最常见的是按钮的Click事件,它是这样委托的:this.button1.Click += new System.EventHandler(this.button1_Click);按按钮后就会出发button1_Click方法进行处理。EventHandler就是系统类库里已经声明的一个委托。

三、自定义事件及其处理
EventHandler以及其它自定义的事件委托都是一类特殊的委托,他们有相同的形式:

delegate void 事件委托名(object sender,EventArgs e);

object用来传递事件的发生者,比如二中的Button控件就是一个事件发生者;EventArgs用来传递事件的细节。

例程二:

程序代码

  1 None.gif using  System;
  2 None.gif
  3 None.gif namespace  最简单的自定义事件
  4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  5ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
  6InBlock.gif/// 事件发送类
  7ExpandedSubBlockEnd.gif/// </summary>

  8InBlock.gifclass Class1
  9ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 10InBlock.gifpublic delegate void UserRequest(object sender,EventArgs e); //定义委托
 11InBlock.gifpublic event UserRequest OnUserRequest; //定义一个委托类型的事件
 12InBlock.gif
 13InBlock.gifpublic void run()
 14ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 15InBlock.gifwhile(true)
 16ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 17InBlock.gifif(Console.ReadLine()=="a")
 18ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{//事件监听
 19InBlock.gifOnUserRequest(this,new EventArgs()); //产生事件
 20ExpandedSubBlockEnd.gif}

 21ExpandedSubBlockEnd.gif}

 22ExpandedSubBlockEnd.gif}

 23ExpandedSubBlockEnd.gif}

 24InBlock.gif
 25ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
 26InBlock.gif/// 事件接收类
 27ExpandedSubBlockEnd.gif/// </summary>

 28InBlock.gifclass Class2
 29ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 30InBlock.gifstatic void Main(string[] args)
 31ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 32InBlock.gifClass1 c1 = new Class1();
 33InBlock.gifc1.OnUserRequest += new Class1.UserRequest(c1_OnUserRequest); //委托实例化后绑定到事件
 34InBlock.gifc1.run();
 35ExpandedSubBlockEnd.gif}

 36InBlock.gif
 37InBlock.gifprivate static void c1_OnUserRequest(object sender, EventArgs e)
 38ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{//事件处理方法
 39InBlock.gifConsole.WriteLine("\t你触发了事件!");
 40ExpandedSubBlockEnd.gif}

 41ExpandedSubBlockEnd.gif}

 42ExpandedBlockEnd.gif}

 43 None.gif例程三:
 44 None.gif
 45 None.gif程序代码
 46 None.gif
 47 None.gif using  System;
 48 None.gif
 49 None.gif namespace  带事件数据的事件
 50 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 51ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
 52InBlock.gif/// 带事件数据的事件类,从EventArgs继承
 53ExpandedSubBlockEnd.gif/// </summary>

 54InBlock.gifclass OnUserRequestEventArgs:EventArgs
 55ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 56InBlock.gifprivate string inputText;
 57InBlock.gifpublic string InputText
 58ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 59InBlock.gifget
 60ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 61InBlock.gifreturn inputText;
 62ExpandedSubBlockEnd.gif}

 63InBlock.gifset
 64ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 65InBlock.gifinputText = value;
 66ExpandedSubBlockEnd.gif}

 67ExpandedSubBlockEnd.gif}

 68ExpandedSubBlockEnd.gif}

 69InBlock.gif
 70ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
 71InBlock.gif/// 事件发送类
 72ExpandedSubBlockEnd.gif/// </summary>

 73InBlock.gifclass Class1
 74ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 75InBlock.gifpublic delegate void UserRequest(object sender,OnUserRequestEventArgs e);
 76InBlock.gifpublic event UserRequest OnUserRequest;
 77InBlock.gifpublic void run()
 78ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 79InBlock.gifwhile(true)
 80ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 81InBlock.gifConsole.WriteLine("请输入内容:");
 82InBlock.gifstring a=Console.ReadLine();
 83InBlock.gif//if(a=="a")
 84InBlock.gif//{
 85InBlock.gifOnUserRequestEventArgs e1 = new OnUserRequestEventArgs();
 86InBlock.gife1.InputText = a;
 87InBlock.gifOnUserRequest(this,e1);
 88InBlock.gif//}
 89ExpandedSubBlockEnd.gif}

 90ExpandedSubBlockEnd.gif}

 91ExpandedSubBlockEnd.gif}

 92InBlock.gif
 93ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
 94InBlock.gif/// 事件接收类
 95ExpandedSubBlockEnd.gif/// </summary>

 96InBlock.gifclass Class2
 97ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 98InBlock.gif[STAThread]
 99InBlock.gifstatic void Main(string[] args)
100ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
101InBlock.gifClass1 c1 = new Class1();
102InBlock.gifc1.OnUserRequest += new Class1.UserRequest(c1_OnUserRequest);
103InBlock.gifc1.run();
104ExpandedSubBlockEnd.gif}

105InBlock.gif
106InBlock.gifprivate static void c1_OnUserRequest(object sender, OnUserRequestEventArgs e)
107ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
108InBlock.gifConsole.WriteLine("\t你输入的是:"+e.InputText);
109ExpandedSubBlockEnd.gif}

110ExpandedSubBlockEnd.gif}

111ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/yunfeng8967/articles/1114696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值