C#编程之委托与事件(一)
本文试图在.net Framework环境下,使用C#语言来描述委托、事件的概貌。希望本文能有助于大家理解委托、事件的概念,理解委托、事件的用途,理解它的C#实现方法,理解委托与事件为我们带来的好处。C#是一种新的语言,希望大家能通过本文清楚地看到这些,从而可以对委托、事件等技术进行更深入的理解和探索。
一. 委托
委托的本质
--在C#中,委托是一个特殊的类;
--在某种程度上,相当于C++的函数指针;
--在某种程度上,相当于接口(Interface);
委托的定义
--关键字:delegate
--public delegate void MyDelegate(string message);
注:在这里我们先了解一个概念,什么是函数签名?(在这里我不做过多解释,大家知道这个概念就行)。
使用委托
我们先来看看一个小的委托示例:
平时,如果说我们要设计一个做简单加减运算的方法,通常是怎么做的呢?看看下面代码:
上面的代码只要是学过程序的人都能看懂,也写得出,不过我们怎么通过委托来处理+,-运算呢?请看下面定义:
上面定义一个返回int类型需要两个int参数的委托。Operator里提供了一个操作方法带有一个委托参数。那通过委托怎么来处理这个简单的运算呢?好,现在我们来修改我们之前定义的主方法,如下:
--返回值:int; 参数:int ,int ;
只要符合该签名的方法,都可以赋给此委托:从上面不难看出,我要要创建一委托,则如下定义:
--委托链:实际上委托实例就是一个委托链,+=代表增加委托实例到委托链中,相反-=则代表去掉该委托实例。
委托的意义之一
--委托可以使得程序的复用程度提高;
--委托在一定程度上想当于接口;
例如:前面例子中的方法Operate(),由于接受的是一个委托类型;那么,我们可以对委托类型赋予不同的方法,来改变Operate()的性质。
我们在来看看另外一个示例:
--我们想输出一串数字,从0-100;
--对于输出的要求有三种;
-1、输出到控制台
-2、输出到窗体中的ListBox中;
-3、输出到文本文件中;
解决方案:
--使用委托和接口, 代码如下:
一. 委托
委托的本质
--在C#中,委托是一个特殊的类;
--在某种程度上,相当于C++的函数指针;
--在某种程度上,相当于接口(Interface);
委托的定义
--关键字:delegate
--public delegate void MyDelegate(string message);
注:在这里我们先了解一个概念,什么是函数签名?(在这里我不做过多解释,大家知道这个概念就行)。
使用委托
我们先来看看一个小的委托示例:
平时,如果说我们要设计一个做简单加减运算的方法,通常是怎么做的呢?看看下面代码:
1
class Program
2
{
3
/// <summary>
4
/// 加法运算
5
/// </summary>
6
/// <param name="x">x</param>
7
/// <param name="y">y</param>
8
/// <returns></returns>
9
private static int Add(int x, int y)
10
{
11
int result = x + y;
12
Console.WriteLine("x + y = {0}",result);
13
return result;
14
}
15![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
16
/// <summary>
17
/// 减法运算
18
/// </summary>
19
/// <param name="x">x</param>
20
/// <param name="y">y</param>
21
/// <returns></returns>
22
private static int Sub(int x, int y)
23
{
24
int result = x - y;
25
Console.WriteLine("x - y = {0}", result);
26
return result;
27
}
28![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
29
static void Main(string[] args)
30
{
31
Add(8, 8);
32
Sub(8, 1);
33
Console.Read();
34
}
35
}
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
2
![](https://image.cha138.com/20210611/bc278b167b2c41888b4bfb7937bc38ea.jpg)
3
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
4
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
5
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
6
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
7
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
8
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
9
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
10
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
11
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
12
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
13
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
14
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
15
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
16
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
17
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
18
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
19
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
20
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
21
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
22
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
23
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
24
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
25
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
26
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
27
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
28
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
29
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
30
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
31
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
32
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
33
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
34
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
35
![](https://image.cha138.com/20210611/6c0ee37aedb0416ca8e40263c4ec04ec.jpg)
上面的代码只要是学过程序的人都能看懂,也写得出,不过我们怎么通过委托来处理+,-运算呢?请看下面定义:
1
namespace DelegateSample1
2
{
3
//定义一委托
4
public delegate int OperationDelegate(int x,int y);
5
public class Operator
6
{
7
private int _x, _y;
8
public Operator(int x, int y)
9
{
10
this._x = x;
11
this._y = y;
12
}
13![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
14
public void Operate(OperationDelegate del)
15
{
16
del(_x, _y);
17
}
18
}
19
}
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
2
![](https://image.cha138.com/20210611/bc278b167b2c41888b4bfb7937bc38ea.jpg)
3
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
4
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
5
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
6
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
7
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
8
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
9
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
10
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
11
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
12
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
13
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
14
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
15
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
16
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
17
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
18
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
19
![](https://image.cha138.com/20210611/6c0ee37aedb0416ca8e40263c4ec04ec.jpg)
上面定义一个返回int类型需要两个int参数的委托。Operator里提供了一个操作方法带有一个委托参数。那通过委托怎么来处理这个简单的运算呢?好,现在我们来修改我们之前定义的主方法,如下:
1
namespace DelegateSample1
2
{
3
class Program
4
{
5
/// <summary>
6
/// 加法运算
7
/// </summary>
8
/// <param name="x">x</param>
9
/// <param name="y">y</param>
10
/// <returns></returns>
11
private static int Add(int x, int y)
12
{
13
int result = x + y;
14
Console.WriteLine("x + y = {0}",result);
15
return result;
16
}
17![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
18
/// <summary>
19
/// 减法运算
20
/// </summary>
21
/// <param name="x">x</param>
22
/// <param name="y">y</param>
23
/// <returns></returns>
24
private static int Sub(int x, int y)
25
{
26
int result = x - y;
27
Console.WriteLine("x - y = {0}", result);
28
return result;
29
}
30![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
31
static void Main(string[] args)
32
{
33
//声明一个委托对象
34
OperationDelegate del = null;
35
del += new OperationDelegate(Add);
36
del += new OperationDelegate(Sub);
37![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
38
Operator op = new Operator(5, 3);
39
op.Operate(del);
40
Console.ReadLine();
41
}
42
}
43
}
44![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
从上面的例子看,委托OperationDelegate代表了一组方法,他们的方法签名是:![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
2
![](https://image.cha138.com/20210611/bc278b167b2c41888b4bfb7937bc38ea.jpg)
3
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
4
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
5
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
6
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
7
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
8
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
9
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
10
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
11
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
12
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
13
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
14
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
15
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
16
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
17
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
18
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
19
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
20
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
21
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
22
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
23
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
24
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
25
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
26
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
27
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
28
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
29
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
30
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
31
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
32
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
33
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
34
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
35
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
36
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
37
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
38
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
39
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
40
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
41
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
42
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
43
![](https://image.cha138.com/20210611/6c0ee37aedb0416ca8e40263c4ec04ec.jpg)
44
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
--返回值:int; 参数:int ,int ;
只要符合该签名的方法,都可以赋给此委托:从上面不难看出,我要要创建一委托,则如下定义:
1
OperationDelegate del += new OperationDelegate(方法名);
从上面可以看到(+=)这个运算符,那是不是也有(-=)这个运算符呢?这就涉及到另外一个概念了--委托链。![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
--委托链:实际上委托实例就是一个委托链,+=代表增加委托实例到委托链中,相反-=则代表去掉该委托实例。
1
OperationDelegate del = null;
2
del += new OperationDelegate(Add); //增加委托实例到委托链
3
del -= new OperationDelegate(Add); //去掉委托实例到
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
2
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
3
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
委托的意义之一
--委托可以使得程序的复用程度提高;
--委托在一定程度上想当于接口;
例如:前面例子中的方法Operate(),由于接受的是一个委托类型;那么,我们可以对委托类型赋予不同的方法,来改变Operate()的性质。
我们在来看看另外一个示例:
--我们想输出一串数字,从0-100;
--对于输出的要求有三种;
-1、输出到控制台
-2、输出到窗体中的ListBox中;
-3、输出到文本文件中;
解决方案:
--使用委托和接口, 代码如下:
1
namespace DelegateSample2
2
{
3
//定义一委托
4
public delegate void ShowNumberDel(object[] items);
5
public class ProcessNumber
6
{
7
private object[] items;
8
public ProcessNumber(int max)
9
{
10
items = new object[max];
11
for (int i = 0; i < max; ++i)
12
{
13
items[i] = i;
14
}
15
}
16![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
17
public void ProcessItems(ShowNumberDel show)
18
{
以上是关于C#编程之委托与事件四的主要内容,如果未能解决你的问题,请参考以下文章
![](https://image.cha138.com/20210611/0775fd11d18b40688ebc1a72b005dcca.jpg)
2
![](https://image.cha138.com/20210611/bc278b167b2c41888b4bfb7937bc38ea.jpg)
3
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
4
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
5
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
6
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
7
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
8
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
9
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
10
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
11
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
12
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)
13
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
14
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
15
![](https://image.cha138.com/20210611/4d7d5583bd5b4f8ca9a4ed6c4bbcf13f.jpg)
16
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
17
![](https://image.cha138.com/20210611/3d7312db589a4176b212ac599f50ecc0.jpg)
18
![](https://image.cha138.com/20210611/ee19e29f11994294942c291cfbf6a9fc.jpg)