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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - LutzMark

charles4抓https请求的注意事项,补充iphone7(ios10系统)无法解密ssl问题 XCode设置自己windows习惯的快捷键(比如Home、End键) MVC项目中ExecutionTimeout不生效的解决方案 坑爹的MSN登录错误80072745 Linq的sum函数InvalidOperationException异常解决办法 LINQ to SQL自定义映射表关系(1:N or 1:1) LINQ to SQL 外键约束的插入及获取主表标识列等问题 Flex中的 for in 与 for each in - LutzMark Flex的DataGrid将水平分隔线设为虚线 - LutzMark - 博客园 解决Flex的DataGrid控件中ItemRender随Scrollbar的滚动发生UI重绘问题 - LutzMark - 博客园 去除ColumnChart自带的阴影效果 - LutzMark - 博客园 IIS7中配置WebOrb支持RTMPT 使用DataAdpater自动批量更新DataSet中的数据到数据库 委托的Invoke 和 BeginInvoke 与Control的Invoke和BeginInvoke(转-因为写得很好) 控制台测试异步委托 一篇好文,以在迷茫时阅读 驳《IT开发工程师的悲哀》 钢板与表针 不用临时变量,只用11个字符交换两个变量的值——窥视C#编译原理的冰山一角
Delegate与Event的区别
LutzMark · 2011-08-25 · via 博客园 - LutzMark
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegate_vs_Event
{

   class Program
   {
      public delegate string TestDelegate(string input);


      public static TestDelegate myDelegate;
      public static event TestDelegate myEvent;
      static void Main(string[] args)
      {
         ///1.在声明委托和事件的类内部,对于Delegate与Event的实例的初始化和调用操作没有任何区别;
         myDelegate = new TestDelegate(TestEvent);
         myEvent = new TestDelegate(TestEvent);
         myDelegate("Delegate");
         myEvent("Event");


         Subject subject = new Subject();
         Observer ob = new Observer(subject);
         subject.Start();
         Console.Read();
      }

      static string TestEvent(string param)
      {
         Console.WriteLine("Hello " + param);
         return string.Empty;
      }
   }

   class Subject
   {
      public delegate void TestHandleForReg(string param);
      public event TestHandleForReg TestHandleEvent;
      public TestHandleForReg TestHandleDelegate;
      public void Start()
      {
         Console.WriteLine("Call external method result:");
         TestHandleEvent("Start");
         Console.WriteLine();

         ///2.对于Delegate与Event的实例,其订阅(Subscribe,即+=)或取消订阅(Unsubscribe,即-=)的操作没有任何区别;
         TestHandleDelegate += Method_Subject1;
         TestHandleDelegate += Method_Subject2;
         TestHandleDelegate -= Method_Subject2;
         TestHandleEvent += Method_Subject1;
         TestHandleEvent += Method_Subject2;
         TestHandleEvent -= Method_Subject2;

         Console.WriteLine("Call internal method result by [Delegate]  ");
         TestHandleDelegate("Delegate");
         Console.WriteLine();


         Console.WriteLine("Call internal method result by [Event]  ");
         TestHandleEvent("Event");
         Console.WriteLine();
      }
      public void Method_Subject1(string str)
      {
         Console.WriteLine("Method_Subject1");
      }
      public void Method_Subject2(string str)
      {
         Console.WriteLine("Method_Subject2");
      }

      public static string Static_Method_Subject()
      {
         return "Static_Method_Subject";
      }
   }
   class Observer
   {
      public Observer(Subject subject)
      {

         subject.TestHandleEvent += Method_Observer;
         subject.TestHandleDelegate += Method_Observer;
         ///3.Delegate与Event唯一区别就在于以下2点:Event提供了更严格的封装访问控制,其目的是为了防止其他类破坏委托的多播链,故不允许在声明事件的类外初始化或调用.
         ///即除了声明Event的类,其它类只能订阅(Subscribe,即+=)或取消订阅(Unsubscribe,即-=)该Event;
         ///   /*1*/
         ///subject.TestHandleEvent = new Subject.TestHandleForReg(Method_Observer);
         /// subject.TestHandleDelegate = new Subject.TestHandleForReg(Method_Observer);///Error:The event 'Delegate_vs_Event.Subject.TestHandleEvent' can only appear on the left hand side of += or -= (except when used from within the type 'Delegate_vs_Event.Subject')	
         ///    /*2*/
         ///subject.TestHandleDelegate("test");
         /// subject.TestHandleEvent("test");///Error:The event 'Delegate_vs_Event.Subject.TestHandleEvent' can only appear on the left hand side of += or -= (except when used from within the type 'Delegate_vs_Event.Subject')	

      }
      public void Method_Observer(string info)
      {
         Console.WriteLine("Observer's method ["+info+"]");
      }
   }

}


1.在声明委托和事件的类内部,对于Delegate与Event的实例的初始化和调用操作没有任何区别;
2.对于Delegate与Event的实例,其订阅(Subscribe,即+=)或取消订阅(Unsubscribe,即-=)的操作没有任何区别;
3.Delegate与Event唯一区别就在于以下2点:Event提供了更严格的封装访问控制,其目的是为了防止其他类破坏委托的多播链,故不允许在声明事件的类外初始化或调用.即除了声明Event的类,其它类只能订阅(Subscribe,即+=)或取消订阅(Unsubscribe,即-=)该Event;