惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - AGPSky

资源文件配置和使用 操作App.config与Web.config文件 正则表达式30分钟入门教程(第二版) C#命名规则、开发习惯和风格 Linq to SQL vs2005和vs2008默认建立web site和web application 生成的webform源码的一点区别 - AGPSky 安装VS2005 SP1时失败(错误 1718。文件被数字签名策略拒绝) gridview 增加行,进行数据汇总 - AGPSky - 博客园 利用DAAB 获取存储过程返回值的方法 - AGPSky - 博客园 【转】C#委托,事件理解入门 (译稿) 【转】C# 中的委托和事件 【转】浅析C#的事件处理和自定义事件[object sender , EventArgs e] [转】object sender,EventArgs e的一些讲解 js的prototype的例子三 js的prototype的例子二 js的prototype的例子一 把控件动态添加到页面中 petshop4.0网上资料 Webdiyer的分页控件+通用存储过程+查询+ajax分页
【转】理解委托与事件的好文章
AGPSky · 2007-10-30 · via 博客园 - AGPSky

1.委派的实现过程。

首先来看一下委派,委派其实就是方法的传递,并不定义方法的实现。事件其实就是标准化了的委派,为了事件处理过程特制的、稍微专业化一点的组播委派(多点委派)。下面举一个例子,我觉得把委派的例子和事件的例子比较,会比较容易理解。

using System;
class Class1
{

       delegate int MathOp(int i1,int i2);

       static void Main(string[] args)
       {

               MathOp op1=new MathOp(Add);

               MathOp op2=new MathOp(Multiply);

               Console.WriteLine(op1(100,200));

              Console.WriteLine(op2(100,200));

               Console.ReadLine();

       }

       public static int Add(int i1,int i2)       {

               return i1+i2;

       }

       public static int Multiply(int i1,int i2)

       {

              return i1*i2;

       }

}

首先代码定义了一个委托mathop,其签名匹配与两个函数add()和multiply()的签名(也就是其带的参数类型数量相同):

delegate int MathOp(int i1,int i2);

main()中代码首先使用新的委托类型声明一个变量,并且初始化委托变量.注意,声明时的参数只要使用委托传递的函数的函数名,而不加括号:

mathop op1=new MathOp(Add);

(或为mathop op1=new MathOp(Multiply);)

委托传递的函数的函数体:

public static int Add(int i1,int i2)

{

       return i1+i2;

}

public static int Multiply(int i1,int i2)

{

      return i1*i2;

}

然后把委托变量看作是一个函数名,将参数传递给函数。 Console.WriteLine(op1(100,200));

console.writeline(op2(100,200));

2.事件的实现过程

using System;

class Class1
{

       static void Main(string[] args)

       {

               Student s1=new Student();

               Student s2=new Student();

               s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

               s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

               s1.Register();

               s2.Register();

               Console.ReadLine();

       }

       static void Student_RegisterOK()

       {

              Console.WriteLine("Hello");

       }

}

class Student
{

       public delegate void DelegateRegisterOkEvent();

       public event DelegateRegisterOkEvent RegisterOK;

       public string Name;

       public void Register()

       {

               Console.WriteLine("Register Method");

              RegisterOK();

       }

}

在student类中,先声明了委托delegateregisterokevent(),然后使用event和要使用的委托类型(前面定义的delegateregisterokevent委托类型)声明事件registerok(可以看作是委托的一个实例。):

public delegate void DelegateRegisterOkEvent();

public event DelegateRegisterOkEvent RegisterOK;

然后在main()函数中,实例化student类,然后s1.registerok事件委托给了student_registerok 方法。通过“+=”(加等于)操作符非常容易地为.Net对象中的一个事件添加一个甚至多个响应方法;还可以通过非常简单的“-=”(减等于)操作符取消这些响应方法。

然后,当调用s1.register()时,事件s1.registerok发生。
来源:http://www.aspxboy.com/private/466/default.aspx