A13_委托与事件的区别

来源:互联网 发布:网络管理结构的重要性 编辑:程序博客网 时间:2024/06/06 01:32

/* *  * 事件不能声明局部实例 * 事件不能作为方法的参数 *  *  * ***/using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace A13_DifferentBetweenDelegateAndEvent{    class Demo1    {        public Action actHandler;       //委托        public event Action eveEvent;   //事件        public Demo1()        {            //委托注册            actHandler += TestMethod1;            actHandler += TestMethod2;            //事件注册            eveEvent += TestMethod1;            eveEvent += TestMethod2;        }        public void TestMethod1()        {            Console.WriteLine("TestMethod1");        }        public void TestMethod2()        {            Console.WriteLine("TestMethod2");        }        public void Test1()        {            if (actHandler!=null)            {                actHandler();            }            if (eveEvent != null)            {                eveEvent();            }        }          public void Test2()        {            //定义委托的局部实例            Action actHandler2 = TestMethod1;            actHandler2 += TestMethod2;        }        static void Main(string[] args)        {            Demo1 obj = new Demo1();            obj.Test1();        }    }}











原创粉丝点击