今天学习一下好久都不愿意碰的Delegate,虽然不是很明白,还是写了一个小例子(参考园子里的朋友们的),希望加深一下印象.
朋友们说它类似于C++的函数指针,指向一个函数,运行的时候可以调用这个函数.
步骤:
1. 声明一个delegate对象。
2. 实现和delegate具有相同参数和返回值的函数实现(可以是静态和非静态的)。
3. 产生一个delegate对象的时候,把你刚刚实现的函数作为参数传给他的构造函数。
代码如下:
Default.aspx.cs中:
public partial class _Default : System.Web.UI.Page
{
//第一步,声明一个delegate对象
public delegate void Mydelegate(string mydelegate);
protected void Page_Load(object sender, EventArgs e)
{
//第三步 产生一个delegate对象的时候,把你刚刚实现的函数(方法)作为参数传给他的构造函数。
Mydelegate hello = new Mydelegate(Hello);
hello("hello");
Mydelegate statichello = new Mydelegate(staticHello);
statichello("beijing");
}
//第二步 实现和delegate具有相同参数和返回值的函数实现(非静态的)
//这里的参数是mydelegate,返回值是void
public void Hello(string mydelegate)
{
Response.Write(mydelegate);
}
//第二步 实现和delegate具有相同参数和返回值的函数实现(静态的)
//这里的参数是mydelegate,返回值是void
public static void staticHello(string mydelegate)
{
HttpContext.Current.Response.Write(mydelegate);
}
}
输出结果:hellobeijing
参考内容:http://www.cnblogs.com/cuihongyu3503319/archive/2007/04/29/732376.html
朋友们说它类似于C++的函数指针,指向一个函数,运行的时候可以调用这个函数.
步骤:
1. 声明一个delegate对象。
2. 实现和delegate具有相同参数和返回值的函数实现(可以是静态和非静态的)。
3. 产生一个delegate对象的时候,把你刚刚实现的函数作为参数传给他的构造函数。
代码如下:
Default.aspx.cs中:
public partial class _Default : System.Web.UI.Page
{
//第一步,声明一个delegate对象
public delegate void Mydelegate(string mydelegate);
protected void Page_Load(object sender, EventArgs e)
{
//第三步 产生一个delegate对象的时候,把你刚刚实现的函数(方法)作为参数传给他的构造函数。
Mydelegate hello = new Mydelegate(Hello);
hello("hello");
Mydelegate statichello = new Mydelegate(staticHello);
statichello("beijing");
}
//第二步 实现和delegate具有相同参数和返回值的函数实现(非静态的)
//这里的参数是mydelegate,返回值是void
public void Hello(string mydelegate)
{
Response.Write(mydelegate);
}
//第二步 实现和delegate具有相同参数和返回值的函数实现(静态的)
//这里的参数是mydelegate,返回值是void
public static void staticHello(string mydelegate)
{
HttpContext.Current.Response.Write(mydelegate);
}
}
输出结果:hellobeijing
参考内容:http://www.cnblogs.com/cuihongyu3503319/archive/2007/04/29/732376.html
------------------------------------------------------------------------------------------------------
了解了delegate,下面我们来看看,在C#中对事件是如何处理的。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
public class EventTest
{
// 步骤1,定义delegate对象
public delegate void MyEventHandler(object sender, System.EventArgs e);
// 步骤2省略
public class MyEventCls
{
// 步骤3,定义事件处理方法,它与delegate对象具有相同的参数和返回值类// 型
public void MyEventFunc(object sender, System.EventArgs e)
{
Console.WriteLine("My event is ok!");
}
}
// 步骤4,用event关键字定义事件对象
private event MyEventHandler myevent;
private MyEventCls myecls;
public EventTest()
{
myecls = new MyEventCls();
// 步骤5,用+=操作符将事件添加到队列中
this.myevent += new MyEventHandler(myecls.MyEventFunc);
}
// 步骤6,以调用delegate的方式写事件触发函数
protected void OnMyEvent(System.EventArgs e)
{
if(myevent != null)
myevent(this, e);
}
public void RaiseEvent()
{
EventArgs e = new EventArgs();
// 步骤7,触发事件
OnMyEvent(e);
}
public static void Main()
{
EventTest et = new EventTest();
Console.Write("Please input ''a'':");
string s = Console.ReadLine();
if(s == "a")
{
et.RaiseEvent();
}
else
{
Console.WriteLine("Error");
}
}
}
C#中的事件处理实际上是一种具有特殊签名的delegate,象下面这个样子:
public delegate void MyEventHandler(object sender, MyEventArgs e);
其中的两个参数,sender代表事件发送者,e是事件参数类。MyEventArgs类用来包含与事件相关的数据,所有的事件参数类都必须从System.EventArgs类派生。当然,如果你的事件不含参数,那么可以直接用System.EventArgs类作为参数。
就是这么简单,结合delegate的实现,我们可以将自定义事件的实现归结为以下几步:
1.定义delegate对象类型,它有两个参数,第一个参数是事件发送者对象,第二个参数是事件参数类对象。
2.定义事件参数类,此类应当从System.EventArgs类派生。如果事件不带参数,这一步可以省略。
3.定义事件处理方法,它应当与delegate对象具有相同的参数和返回值类型。
4. 用event关键字定义事件对象,它同时也是一个delegate对象。
5.用+=操作符添加事件到事件队列中(-=操作符能够将事件从队列中删除)。
6.在需要触发事件的地方用调用delegate的方式写事件触发方法。一般来说,此方法应为protected访问限制,既不能以public方式调用,但可以被子类继承。名字是OnEventName。
7. 在适当的地方调用事件触发方法触发事件。
下面是一个简单的例子:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
参考:http://hi.baidu.com/i_sea/blog/item/b3094138cb38f5f5b211c7bc.html