• Delegate与Event的区别[转]


    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;

  • 相关阅读:
    oracle(Xe)数据库远程连接需修改配置参数
    oracl 权限循环查询
    控件网站
    java常用类(1)
    关于webdriver和谷歌浏览器的那些事
    2020年第27周,24.75h,完成计算智能/物联网/数据挖掘大作业
    2020年第26周,24.75h,计算智能的大小作业
    2020年第25周,25.5h,随机过程考试、report和计算智能作业
    2020年24周,11.75h,以完成作业和考试为主,看了一点点论文
    2020年第23周,11h,努力完成课程作业
  • 原文地址:https://www.cnblogs.com/kingangWang/p/2152892.html
Copyright © 2020-2023  润新知