码迷,mamicode.com
首页 > Windows程序 > 详细

C#中的委托和事件

时间:2018-12-08 17:58:52      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:模式   lam   out   method   code   fat   col   summary   lambda   

C#中的委托是一种类型,是方法的一种抽象,事件是一种特殊的委托。

区别在于:委托可以直接赋值和调用,而事件只能在外部进行赋值和调用,事件在外部只能用+=进行订阅。

委托的几种调用方式:

 NoReturnWithPara method = new NoReturnWithPara(ShowPlus);  //实例化委托

         // method(3,4);      

     //method.Invoke(3,4);   

       //method.BeginInvoke(3,4,null,null);    //异步调用

多播委托是不能异步调用的,多播委托用+=,-=去添加和移除多个方法,但是lambda表达式(匿名方法)是移除不了的。

多播委托带返回值,结果是最后那个方法的。

  public class MyDelegate
    {
        public delegate void NoReturnNoPara();
        public delegate void NoReturnWithPara(int x,int y);   //声明委托

        public void Show()
        {
            NoReturnWithPara method = new NoReturnWithPara(ShowPlus);  //实例化委托
           // method(3,4);
            //method.Invoke(3,4);
            //ShowPlus(3,4);
            //method.BeginInvoke(3,4,null,null);    //异步调用


            //多播委托
            method += ShowPlus;
            method += (x, y) => Console.WriteLine("我是{0}和我得啥{1}",x,y);
            method += ShowPlusStatic;
            method += ShowPlusStatic;
            method += ShowPlusStatic;

            method -= ShowPlus;
            method -= ShowPlusStatic;
            //lambda表达式(匿名方法)是无法移除的。
            method -= (x, y) => Console.WriteLine("我是{0}和我得啥{1}", x, y);

            method(7,8);
        //    method.BeginInvoke(7,8,null,null);   //多播委托是不能异步的


            Func<string, int> func = s => s.Length;
         
            func += s => s.Length + 2;
            func += s => s.Length + 3;
            func += s => s.Length + 1;

            int iResult = func("Zain");   //5 多播委托带返回值,结果是最后那个方法的。

        }

        public void ShowPlus(int x,int y)
        {
            Console.WriteLine("打印x,和y的值{0},{1}",x,y);
        }

        public static void ShowPlusStatic(int x, int y)
        {
            Console.WriteLine("static打印x,和y的值{0},{1}", x, y);
        }
    }

 

委托和事件的区别,发布订阅模式(观察者模式),猫叫,老鼠跑,小孩哭,大人醒。

  /// <summary>
  /// 发布者    (发布订阅模式,观察者模式。)
  /// </summary>
    public class Cat
    {
        public Action CatMiaoHandler;
        public event Action CatMiaoHandlerEvent;  //事件就是委托的一个实例,加上event关键字修饰
        public void Miao()
        {
            Console.WriteLine("{0} Miao", this.GetType().Name);

        

            new Neighbor().Awake();
            new Mouse().Run();
            new Dog().Wang();
            new Baby().Cry();
            new Father().Shout();
            new Mother().Whiper();

        }

        public void MiaoDelegate()
        {
            Console.WriteLine("{0} Miao", this.GetType().Name);

            if (CatMiaoHandler != null)
            {
                CatMiaoHandler();
            }

        }

        public void MiaoEvent()
        {
            Console.WriteLine("{0} Miao", this.GetType().Name);

            if (CatMiaoHandlerEvent != null)
            {
                CatMiaoHandlerEvent.Invoke();
            }
            CatMiaoHandlerEvent = null;               //事件内部可以赋值,可以调用

        }
    }
  class Program
    {
       
         
        static void Main(string[] args)
        {
            MyDelegate md = new MyDelegate();
            md.Show();

            Greeting greeting = new Greeting();
            //greeting.SayHi("zain",Greeting.PeopleType.Chinese);

            //greeting.SayHi("Eleven", Greeting.PeopleType.Buluo);



            Action<string> act = new Action<string>(greeting.SayHiChinese);
            greeting.SayHiDelegate("zain",act);





            {

                Cat cat = new Cat();
                cat.Miao();

                Console.WriteLine("******************************Delegate****************");
                cat.CatMiaoHandler = () => Console.WriteLine("这里是Delegate");  //直接给委托赋值
                cat.CatMiaoHandler += new Neighbor().Awake;
                cat.CatMiaoHandler += new Mouse().Run;
                cat.CatMiaoHandler += new Dog().Wang;
                cat.CatMiaoHandler += new Baby().Cry;
                cat.CatMiaoHandler += new Father().Shout;
                cat.CatMiaoHandler += new Mother().Whiper;

                cat.CatMiaoHandler.Invoke();    //直接调用委托
                cat.MiaoDelegate();
            }
            {

                Cat cat = new Cat();
                cat.Miao();

                Console.WriteLine("******************************Event****************");
          //      cat.CatMiaoHandlerEvent = () => Console.WriteLine("这里是Event");  //外部不能直接给事件赋值
                cat.CatMiaoHandlerEvent += new Neighbor().Awake;    //事件的订阅
                cat.CatMiaoHandlerEvent += new Mouse().Run;
                cat.CatMiaoHandlerEvent += new Dog().Wang;
                cat.CatMiaoHandlerEvent += new Baby().Cry;
                cat.CatMiaoHandlerEvent += new Father().Shout;
                cat.CatMiaoHandlerEvent += new Mother().Whiper;

           //     cat.CatMiaoHandlerEvent()   //外部不能直接调用事件
                cat.MiaoEvent();
            }





            Console.ReadKey();
        }



     
    }

 


 

C#中的委托和事件

标签:模式   lam   out   method   code   fat   col   summary   lambda   

原文地址:https://www.cnblogs.com/zhumeiming/p/10088340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!