




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Delegate来谈C# Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delegate这个特性,所以一直没有对它作整理。这两天,我再度翻阅了一些关于Delegate的资料,并开始正式整理这个C#中著名的特性。本文将由浅入深的谈一下Delegate这个特性。一.Delegate是什么? Delegate中文翻译为“委托”。Msdn中对Delegate的解释如下: C#中的委托类似于C或C+中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与C或C+中的
2、函数指针不同,委托是面向对象、类型安全的,并且是安全的。 如果你是第一次接触Delegate这个概念,你可能会对上面这段文字感觉不知所云,不过不要紧,你可以先把Delegate认为就是一个函数指针。 而当你面对一个虚无的概念时,最好的应对方法就是直接看实例。下面一个简单的Delegate使用例子。class Program static void OtherClassMethod() Console.WriteLine(Delegate an other classs method); static void Main(string args) var test = new TestDeleg
3、ate(); test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod); test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod); test.delegateMethod += Program.OtherClassMethod; test.RunDelegateMethods(); class TestDelegate public delegate void DelegateMethod();
4、 /声明了一个Delegate Type public DelegateMethod delegateMethod; /声明了一个Delegate对象 public static void StaticMethod() Console.WriteLine(Delegate a static method); public void NonStaticMethod() Console.WriteLine(Delegate a non-static method); public void RunDelegateMethods() if(delegateMethod != null) Consol
5、e.WriteLine(-); delegateMethod.Invoke();Console.WriteLine(-); 上面是一个Delegate的使用例子,运行看看结果吧。下面我稍微解释一下:【1】public delegate void DelegateMethod();这里声明了一个Delegate的类型,名为DelegateMethod,这种Delegate类型可以搭载:返回值为void,无传入参数的函数。【2】public DelegateMethod delegateMethod;这里声明了一个DelegateMethod的对象(即,声明了某种Delegate类型的对象)。区分
6、:DelegateMethod是类型,delegateMethod是对象。【3】为什么上面说Delegate可以看做是函数指针呢?看下面这段代码:test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod); test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod); test.delegateMethod += Program.OtherClassMethod;这里delegateMethod搭载了
7、3个函数,而且可以通过调用delegateMethod.Invoke();运行被搭载的函数。这就是Delegate可以看作为函数指针的原因。上面这段代码中,delegateMethod只能搭载:返回值为void,无传入参数的函数(见:NonStaticMethod,StaticMethod,OtherClassMethod的定义),这和Delegate类型声明有关(见DelegateMethod的声明:public delegate void DelegateMethod())。【4】Delegate在搭载多个方法时,可以通过+=增加搭载的函数,也可以通过-=来去掉Delegate中的某个函数
8、。二.Delegate和C+中函数指针的区别 Delegate和C+中的函数指针很像,但如果深入对比,发现其实还是有区别的,区别主要有三个方面(参考Stanley B. Lippman的一篇文章) 1) 一个 delegate对象一次可以搭载多个方法(methods),而不是一次一个。当我们唤起一个搭载了多个方法(methods)的delegate,所有方法以其“被搭载到delegate对象的顺序”被依次唤起。 2) 一个delegate对象所搭载的方法(methods)并不需要属于同一个类别。一个delegate对象所搭载的所有方法(methods)必须具有相同的原型和形式。然而,这些方法(
9、methods)可以即有static也有non-static,可以由一个或多个不同类别的成员组成。 3) 一个delegate type的声明在本质上是创建了一个新的subtype instance,该 subtype 派生自 .NET library framework 的 abstract base classes Delegate 或 MulticastDelegate,它们提供一组public methods用以询访delegate对象或其搭载的方法(methods) ,与函数指针不同,委托是面向对象、类型安全并且安全的。 看完上面关于Delegate的介绍,相信大家对它也有所了解了,
10、下面我们将进行更深入地讨论!三.Delegate什么时候该用? 看完上面的介绍,你可以会有一些疑问,为什么会有Delegate?实际中什么时候会用到?什么时候应该去用? 在回答这些问题之前,大家可以先看看下面这段代码:class Program static void Main(string args) var car = new Car(15); new Alerter(car); car.Run(120); class Car public delegate void Notify(int value); public event Notify notifier; private int
11、petrol = 0; public int Petrol get return petrol; set petrol = value; if (petrol 0) Thread.Sleep(500); Petrol-; distance += speed; Console.WriteLine(Car is running. Distance is + distance.ToString(); class Alerter public Alerter(Car car) car.notifier += new Car.Notify(NotEnoughPetrol); public void No
12、tEnoughPetrol(int value) Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(You only have + value.ToString() + gallon petrol left!); Console.ResetColor(); 看完了上面的代码后,你可能会问:为什么不在public int Petrol中直接调用Alerter.NotEnoughPetrol呢?因为Car模块和Alerter模块本身是两个独立的子系统,如果直接调用,耦合性就会增加,这不是我们愿意看到的。 其实以上的代码是设计
13、模式中的观察者模式(观察者模式又称Source/Listener模式)的实现,当汽车在运行中汽油量“public Notify notifier”的IL代码,如图:“public event Notify notifier”的IL代码,如图:差别其实已经很明显了,“public Notify notifier”相当于Class里面的Field,访问级别是public,而“public event Notify notifier”则相当于Property,访问级别是private!由于以上的差别,他们在某些使用上,会稍有不同,详细的可参考shensr写的delegate vs. event。五.Delegate中的Invoke与BeginInvoke方法 简单说一下,Invoke与BeginInvoke都是执行Delegate里的搭载函数,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 来华留学生中级汉语综合课多模态线上教学研究
- 餐饮卫生安全教育培训
- 自我认知与心理健康
- 小班幼儿游戏活动课件设计
- 大班健康:吃进去的食物去哪了
- 解读护理条例案例
- 我爱游泳健康教育指南
- 颈椎影像检查技术课件教学
- 2025年吉林省中考招生考试数学真题试卷(真题+答案)
- 客服培训与发展战略
- 2025至2030内燃机市场发展趋势分析与未来投资战略咨询研究报告
- 2025年陕西延长石油招聘笔试备考题库(带答案详解)
- 机加工工艺培训
- 黄大年式教学团队申报材料
- 出香港货物发票样板样本空白
- 医院免疫室标准化操作程序免疫室内质量控制操作指南(ELISA)人民医院检验科免疫SOP人民医院质量管理体系课件
- 柳州市柳东新区南庆安置区项目工程基坑支护方案
- 卵巢肿瘤ppt课件
- 发电可靠性考试真题及答案
- 工程塑料 第七章特种工程塑料
- 联东U谷厂房销售合同
评论
0/150
提交评论