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

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - 风的影

微服务 口试常见题 SQL 大数据查询如何进行优化? 虚方法(virtual)和抽象方法(abstract)的和接口(interface)的区别 高并发的秒杀 C#算法 口试C#概念 口试Linq题 口试大数据及大并发问题 笔试题多线程 笔试代码考查 SQL笔试基础 口试基础 笔试基础知识 Asp.net项目从Vs2003转换到Vs2005的常见问题大全及解决方法 烦人的网页iframe去除 经典sql注入教程 C# 相当于ASP里Eval中的计算公式的方法(超简单的方法) 自己写的后台静态权限验证类
事件和委托的区别
风的影 · 2018-08-01 · via 博客园 - 风的影

很多人说,事件即委托。真的是这样吗?那么微软为何又要定义事件与委托两个概念呢?
其实事件即委托,在我看来不如这样解释:事件是封装过的委托实例。也就是说,事件是面向对象的产物,它是封装性比较典型的示例。

事件实际上是一个特殊的委托实例,不用事件也没有关系。实际上事件只是削弱了委托的功能,event在编译器角度保护了你程序的安全,因为你只能使用+=、-=来注册事件了,而不能使用 = 为事件关联方法。(在委托中还可以使用=来绑定方法,不过=是一种破坏性代码,不管之前是否已经绑定的有方法了,他都会将其清除)

同一个方法你可以使用委托来调用,你也可以使用事件来调用。那究竟有何不同?
其实事件就是一个狭义的委托,也就是事件是一个用于事件驱动模型的专用委托.你可以在客户代码中直接调用委托来激发委托指向的函数,而事件不可以,事件的触发只能由服务代码自己触发。也就是说在你的代码里委托你不但可以安排谁是它的调用函数,还可以直接调用它,而事件不能直接调用,只能通过某些操作触发。除此之此,事件拥有委托的所有功能,包括多播特性。即事件可以有多个事件处理函数,委托同样也可以是个多播委托.

public class Heater
{
  private int temperature;
  public void BoilWater()
  {
    for (int i = 1; i <= 100; i++)
    {
      temperature = i;
      if (temperature > 95 && BoilEvent != null)
      {
        //执行委托,无需知道要执行哪些方法
        BoilEvent(temperature);
      }
    }
  }
  public delegate void BoilHandle(int param);
  public event BoilHandle BoilEvent;//封装了委托
}
public class Alarm
{
  public void MakeAlert(int param)
  {
    Console.WriteLine("Alarm:水温已经超过{0}度.",param);
  }
}
public class Display
{
  public void ShowMsg(int param)
  {
    Console.WriteLine("Display:水已烧开,当前温度{0}度.", param);
  }
}
[TestClass]
public class UnitTest1
{
  [TestMethod]
  public void TestMethod1()
  {
    Heater heater = new Heater();
    Alarm alarm = new Alarm();
    heater.BoilEvent += alarm.MakeAlert;
    heater.BoilEvent += new Display().ShowMsg;
    heater.BoilWater();
  }
}