C#中委托和事件的区别实例解析

这篇文章主要介绍了C#中委托和事件的区别,并分别以实例形式展示了通过委托执行方法与通过事件执行方法,以及相关的执行流程与原理分析,需要的朋友可以参考下
本文实例分析了C#中委托和事件的区别,分享给大家供大家参考之用。具体如下:

大致来说,委托是一个类,该类内部维护着一个字段,指向一个方法。事件可以被看作一个委托类型的变量,通过事件注册、取消多个委托或方法。本篇分别通过委托和事件执行多个方法,从中体会两者的区别。

一、通过委托执行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Program
{
static void Main(string[] args)
{
Example example = new Example();
example.Go();
Console.ReadKey();
}
}
public class Example
{
public delegate void DoSth(string str);
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
DoSth d = new DoSth(Print);
string str = "Hello,World";
//通过委托的静态方法Invoke触发委托
d.Invoke(str);
}
void Print(string str)
{
Console.WriteLine(str);
}
}

上述代码实现:

① 在CLR运行时,委托DoSth实际上就一个类,该类有一个参数类型为方法的构造函数,并且提供了一个Invoke实例方法,用来触发委托的执行。
② 委托DoSth定义了方法的参数和返回类型
③ 通过委托DoSth的构造函数,可以把符合定义的方法赋值给委托
④ 调用委托的实例方法Invoke执行了方法

但实际上让委托执行方法还有另外一种方式,那就是:委托变量(参数列表)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Example
{
public delegate void DoSth(object sender, EventArgs e);
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
DoSth d = new DoSth(Print);
object sender = 10;
EventArgs e = new EventArgs();
d(sender, e);
}
void Print(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}

上述代码实现:

① 委托DoSth的参数列表和方法Print的参数列表还是保持一致
② 委托DoSth中的参数object sender通常用来表示动作的发起者,EventArgs e用来表示动作所带的参数。

而实际上,委托变量(参数列表),事件就是采用这种形式执行方法的。

二、通过事件执行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Example
{
public delegate void DoSth(object sender, EventArgs e);
public event DoSth myDoSth;
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
DoSth d = new DoSth(Print);
object sender = 10;
EventArgs e = new EventArgs();
myDoSth += new DoSth(d);
myDoSth(sender, e);
}
void Print(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}

上述代码实现:

① 声明了事件myDoSth,事件的类型是DoSth这个委托
② 通过+=为事件注册委托
③ 通过DoSth委托的构造函数为事件注册委托实例
④ 采用委托变量(参数列表)这种形式,让事件执行方法

而且,通过+=还可以为事件注册多个委托。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Example
{
public delegate void DoSth(object sender, EventArgs e);
public event DoSth myDoSth;
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
DoSth d = new DoSth(Print);
DoSth d1 = new DoSth(Say);
object sender = 10;
EventArgs e = new EventArgs();
//为事件注册多个委托
myDoSth += new DoSth(d);
myDoSth += new DoSth(d1);
myDoSth(sender, e);
}
void Print(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
void Say(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}

以上,通过+=为事件注册1个或多个委托实例,实际上,还可以为事件直接注册方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Example
{
public delegate void DoSth(object sender, EventArgs e);
public event DoSth myDoSth;
internal void Go()
{
object sender = 10;
EventArgs e = new EventArgs();
//为事件注册多个委托
myDoSth += Print;
myDoSth += Say;
myDoSth(sender, e);
}
void Print(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
void Say(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}

三、通过EventHandler执行方法

先来看EventHandler的源代码。

1
public delegate void EventHandler(object sender, System.EventArgs e);

可见,EventHandler就是委托。现在就使用EventHandler来执行多个方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Example
{
public event EventHandler myEvent;
internal void Go()
{
object sender = 10;
EventArgs e = new EventArgs();
//为事件注册多个委托
myEvent += Print;
myEvent += Say;
myEvent(sender, e);
}
void Print(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
void Say(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}

总结:

① 委托就是一个类,也可以实例化,

原文地址:https://www.cnblogs.com/0515offer/p/4228555.html