如果你要在这里知道什么叫事件,什么叫委托,那我就要说不好意思了,因为,我不打算在这里阐述,也阐述不好!所以,只能谈谈自己个人对他们的理解,起个抛砖引玉的作用,希望能得到大家的高见!
委托也其实就是个指针,但是,他严格定义了它所指向方法的定义(如参数类型,个数,返回值等),当实例化委托时,只能将符合它所要求的方法做为参数传递给它;
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
public delegate void someEventHandler(object sender,eventArgs er);
之后引用委托实例时,将直接调用用来实例化此委托的方法!这也达到了隐藏具体方法的目的!
事件,我更愿意说它是个"动作",是指接通事件发生器和事件接收器,起到一个"通知"的作用!在事件发生器端,当发生"事件"(这个"事件"不是这里讨论的事件,而是指:发生了某个方法调用,比如当计时器计时到一定时间)时将触发事件,也就是通知了事件接收器,之后事件接收器将执行事件处理程序!事件触发器完成:1,注册事件;2,当事件发生时,执行事件处理函数
具体的实例代码 如下 :
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
using System.Threading;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace Event
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public delegate void someEventHandler(object sender,eventArgs er);
10
11
public class eventArgs : EventArgs //用来传递事件发生后的相关参数
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
private string _msg;
14
public eventArgs():base()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
}
17
public eventArgs(string msg):base()
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
this._msg = msg;
20
}
21
public string Msg
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _msg; }
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._msg = value; }
25
}
26
}
27
public class Come //事件发生器:用来触发事件
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
public event someEventHandler Action;
30
public void start()//启动"触发事件",每过一秒钟,启动一次事件
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
eventArgs er = new eventArgs("I'm the 'eventArgs' from 'Come'");
33
for (int i = 0; i < 5; i++)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
OnAction(this,er);
36
Console.WriteLine("Sleeping a moment,then I'll come again!");
37
Thread.Sleep(1000);
38
}
39
}
40
public void OnAction(object sender, eventArgs er)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
if (Action != null) //事件不能为空
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
Action(sender, er);
45
}
46
else
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
Console.WriteLine("the 'Action is null");
49
}
50
}
51
public string str
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return "I'm the 'Event' sender:Come!;"; }
54
}
55
}
56
public class doWhenCome //事件接收器:1,注册事件;2,当事件发生时,执行事件处理函数
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
public doWhenCome(Come come)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
come.Action += new someEventHandler(doSomething);
61
}
62
public void doSomething(object sender, eventArgs er)//事件处理函数只根据事件发生器传递过来的信息(sender,eventArgs)进行相关处理!而无权改动;
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
Come c=(Come)sender;
65
Console.WriteLine("Event come,the sender said :"+c.str.ToString ());
66
Console.WriteLine("Event come,the eventArgs said :" + er.Msg.ToString());
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
}
70
}
71![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)