zoukankan      html  css  js  c++  java
  • C#中的委托和事件

    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();
            }
    
    
    
         
        }

    sometimes the hardest part isn't letting go,but rather start over
  • 相关阅读:
    河北省重大技术需求征集系统(10)
    河北省重大技术需求征集系统(9)
    大三上学期周总结
    河北省重大技术需求征集系统(8)
    《代码大全》阅读笔记(三)
    河北省重大技术需求征集系统(7)
    河北省重大技术需求征集系统(6)
    河北省重大技术需求征集系统(5)
    【自动化】Aritest+python实现客户端UI自动化
    【自动化】Monkey自动化测试实现总结
  • 原文地址:https://www.cnblogs.com/zhumeiming/p/10088340.html
Copyright © 2011-2022 走看看