Delegate中文翻译为“委托”。MSDN中对Delegate的解释如下:

C#中的委托类似于C或C++中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与C或C++中的函数指针不同,委托是面向对象、类型安全的,并且是安全的。

如果你是第一次接触Delegate这个概念,你可能会对上面这段文字感觉不知所云,不过不要紧,你可以先把Delegate认为就是一个函数指针。

而当你面对一个虚无的概念时,最好的应对方法就是直接看实例。下面一个简单的Delegate使用例子。

class Program
{
static void OtherClassMethod()
{
Console.WriteLine("Deledate an other class's Method");
}
static void Main(string[] args)
{
var test = new TestDelegate();
test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod); test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod); test.delegateMethod += OtherClassMethod; test.RunDelegateMethods();
Console.ReadKey(); } class TestDelegate
{
public delegate void DelegateMethod(); //声明了一个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)
{
Console.WriteLine("---------");
delegateMethod.Invoke();
Console.WriteLine("---------");
}
}
}
}

上面是一个Delegate的使用例子,运行看看结果吧。下面我稍微解释一下:

【1】public delegate void DelegateMethod();这里声明了一个Delegate的类型,名为DelegateMethod,这种Delegate类型可以搭载:返回值为void,无传入参数的函数。

【2】public DelegateMethod delegateMethod;这里声明了一个DelegateMethod的对象(即,声明了某种Delegate类型的对象)。

区分:DelegateMethod是类型,delegateMethod是对象。

【3】为什么上面说Delegate可以看做是函数指针呢?看下面这段代码:

test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod);
test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod);
test.delegateMethod += Program.OtherClassMethod;

这里delegateMethod搭载了3个函数,而且可以通过调用delegateMethod.Invoke();运行被搭载的函数。这就是Delegate可以看作为函数指针的原因。上面这段代码中,delegateMethod只能搭载:返回值为void,无传入参数的函数(见:NonStaticMethod,StaticMethod,OtherClassMethod的定义),这和Delegate类型声明有关(见DelegateMethod的声明:public delegate void DelegateMethod())。

【4】Delegate在搭载多个方法时,可以通过+=增加搭载的函数,也可以通过-=来去掉Delegate中的某个函数。

二.Delegate和C++中函数指针的区别

Delegate和C++中的函数指针很像,但如果深入对比,发现其实还是有区别的,区别主要有三个方面(参考Stanley B. Lippman的一篇文章)

1) 一个 delegate对象一次可以搭载多个方法(methods),而不是一次一个。当我们唤起一个搭载了多个方法(methods)的delegate,所有方法以其“被搭载到delegate对象的顺序”被依次唤起。

2) 一个delegate对象所搭载的方法(methods)并不需要属于同一个类别。一个delegate对象所搭载的所有方法(methods)必须具有相同的原型和形式。然而,这些方法(methods)可以即有static也有non-static,可以由一个或多个不同类别的成员组成。

3) 一个delegate type的声明在本质上是创建了一个新的subtype instance,该 subtype 派生自 .NET library framework 的 abstract base classes Delegate 或 MulticastDelegate,它们提供一组public methods用以询访delegate对象或其搭载的方法(methods) ,与函数指针不同,委托是面向对象、类型安全并且安全的。

看完上面关于Delegate的介绍,相信大家对它也有所了解了,下面我们将进行更深入地讨论!

三.Delegate什么时候该用?

看完上面的介绍,你可以会有一些疑问,为什么会有Delegate?实际中什么时候会用到?什么时候应该去用? 在回答这些问题之前,大家可以先看看下面这段代码

class Program
{
static void Main(string[] args)
{
var car = new Car();
new Alerter(car);
car.Run();
Console.ReadKey();
} }
class Car
{
public delegate void Notify(int value);
public event Notify notifier; private int petrol = ; public int Petrol
{
get { return petrol; }
set
{
petrol = value;
if (petrol < ) ////当petrol的值小于10时,出发警报
{
if (notifier != null)
{
notifier.Invoke(Petrol);
}
}
}
} public Car(int petrol)
{
Petrol = petrol;
} public void Run(int speed)
{
int distance = ;
while (Petrol > )
{
Thread.Sleep();
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 NotEnoughPetrol(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模块本身是两个独立的子系统,如果直接调用,耦合性就会增加,这不是我们愿意看到的。

其实以上的代码是设计模式中的观察者模式(观察者模式又称Source/Listener模式)的实现,当汽车在运行中汽油量<10时,警报器便会发出警报。在上面代码中,Delegate相当于一个存放回调函数的函数指针,使用Delegate,我们可以非常方便地实现观察者模式。而其实,在需要使用回调函数时,我们都可以考虑使用Delegate。

不知道你有没有发现在上面的代码中还有一个问题呢?

public event Notify notifier;
上面的代码中,我们定义了一个Event,而事实上:

public Notify notifier;
这样写,也完全可以满足我们的需求,这就引出了我们的另一个问题,Delegate和Event!

四.Delegate与Event

【1】Delegate和Event的关系

【2】Delegate和Event配合使用的效果

五.Delegate中的Invoke与BeginInvoke方法

简单说一下,Invoke与BeginInvoke都是执行Delegate里的搭载函数,而不同的是:Invoke是一个同步方法,BeginInvoke是一个异步方法。关于这个,有一篇文章《Invoke and BeginInvoke》,对此介绍的比较详细,这里就不多说了。

六.小结

回顾一下,到底什么时候我们可能会用到Delegate:

【1】.当我们在C#中需要类似函数指针这样东西时。

【2】.当我们需要使用回调函数的时候。

【3】.需要异步调用的时候。

【4】.实现观察者模式的时候。

【5】.处理事件响应的时候。

以上内容均为个人看法,如果有错漏,请各位及时指出:>

转载请说明出处,谢谢![hyddd(http://www.cnblogs.com/hyddd/)]

参考资料

【1】Msdn委托教程Msdn事件教程

【2】《深入探索面向对象事件(Delegate)机制

【3】《对.net事件的看法

【4】《Invoke and BeginInvoke

【5】《delegate vs. event

【6】《C#事件(event)解析

【7】《C# Delegate 简介

转:C# Delegate委托 1的更多相关文章

  1. 快速理解C#高级概念(一) Delegate委托

    做.NET开发很久,最近重新温习<C#高级编程>一书.发现很多曾经似懂非懂的问题,其实也是能够慢慢钻研慢慢理解的. 所以,打算开写<C#高级编程系列>博文.其中会借鉴<C ...

  2. WPF Delegate委托整理

    那啥,是从这里整理出来的,感谢Rising_Sun,整理的过于简单,看不明白的戳这里 using System; using System.Collections.Generic; using Sys ...

  3. 关于js模拟c#的Delegate(委托)实现

    这是我的第一篇博文,想来讲一讲js的函数.我的标题是js模拟c#的Delegate. 一.什么是Delegate(委托) 在jquery中有delegate函数,作用是将某个dom元素的标签的事件委托 ...

  4. 使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神

    使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神 前言 接上一篇 使用 EPPlus 封装的 excel 表格导入功能 (一) 前一篇的是大概能用但是 ...

  5. 【UE4 C++ 基础知识】<8> Delegate 委托

    概念 定义 UE4中的delegate(委托)常用于解耦不同对象之间的关联:委托的触发者不与监听者有直接关联,两者通过委托对象间接地建立联系. 监听者通过将响应函数绑定到委托上,使得委托触发时立即收到 ...

  6. C#delegate委托

    类似函数,却没有语句体. using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  7. C# EventHandler and Delegate(委托的使用)

    委托的声明 public delegate void MyDelegate(string str); 注 1.委托的定义和方法的定义类似,只是在前面加了一个delegate,但委托不是方法,它是一种特 ...

  8. C# 匿名方法 委托 Action委托 Delegate委托

    原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿 ...

  9. 转载: jQuery事件委托( bind() \ live() \ delegate()) [委托 和 绑定的故事]

    转载:http://blog.csdn.net/zc2087/article/details/7287429 随着DOM结构的复杂化和Ajax等动态脚本技术的运用,事件委托自然浮出了水面.jQuery ...

随机推荐

  1. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第四集之安装Linux】

    1,确保Linux镜像的路径存在 2,启动 3,在真实机情况下,进入BIOS修改安装操作系统的路径[记住:虚拟机不需要这一步.] 如果是真实机安装Linux,默认是从硬盘中安装,而不是从光盘.这就需要 ...

  2. day66 模板小结 [母板继承,块,组件]

    小结: day65 1. 老师编辑功能写完 1. magic2函数 --> 用两层for循环解决 全栈8期之殇 问题 2. 模板语言 in 语法 {% if xx in xx_list %} { ...

  3. Shiro笔记(五)Shiro授权

    Shiro授权 也叫访问控制,即在应用中控制谁能访问那些资源(如访问页面.编辑数据.页面操作等).在授权中需要了解几个关键对象:主体(subject).资源(resource).权限(Permissi ...

  4. 转 国内的go get问题的解决

    转 国内的go get问题的解决     go get golang.org/x 包失败解决方法 由于各种问题,国内使用 go get 安装 golang 官方包可能会失败,如我自己在安装 colli ...

  5. datetime.timedelta类

    datetime.timedelta对象代表两个时间之间的时间差,两个date或datetime对象相减就可以返回一个timedelta对象. Python中datetime模块中的timedelta ...

  6. 003.Keepalived搭建LVS高可用集群

    一 基础环境 1.1 IP规划 OS:CentOS 6.8 64位 节点类型 IP规划 主机名 类型 主 Director Server eth0:172.24.8.10 DR1 公共IP eth1: ...

  7. String、StringBuffer、StringBuilder的比较

    看String类的定义:public final class String...{private final char value[];} 看AbstractStringBuilder类的定义:abs ...

  8. 60.Search Insert Position.md

    描述 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 您在真实的面试中是否遇到过这个题? 样例 Given ...

  9. 牛客国庆集训派对Day4.B.异或求和(按位统计)

    题目链接 刷牛客一战到底做到的,感觉还挺有趣... \(Description\) 求给定\(n\)及序列\(A_i\),求\[\sum_{i\lt j\lt k}(A_i\oplus A_j)(A_ ...

  10. 潭州课堂25班:Ph201805201 django框架 第九课 模型补充 博客小案例 (课堂笔记)

    聚合查询: 分组查询: annotate() 方法 例:查询某学院学生人数,(一对多查询) 以字典的形式输出 annotate(统计 ‘关联学生字段 出现的次,).字典形式(键,值) 例:查询每项课程 ...