• .NET基础拾遗(4)委托和事件1


    一、委托初窥:一个拥有方法的对象

    (1)本质:持有一个或多个方法的对象;委托和典型的对象不同,执行委托实际上是执行它所“持有”的方法。

      (2)如何使用委托?

        ①声明委托类型(delegate关键字)

        ②使用该委托类型声明一个委托变量

        ③为委托类型增加方法

        ④调用委托执行方法

    (3)委托的恒定性:

      组合委托、为委托+=增加方法以及为委托-=移除方法让我们看起来像是委托被修改了,其实它们并没有被修改。事实上,委托是恒定的

      在为委托增加和移除方法时实际发生的是创建了一个新的委托,其调用列表是增加和移除后的方法结果。

    delegate void PrintFunction();
    
    class Test
    {
       public void Print1()//实例方法
       {
          Console.WriteLine( "Print1 -- instance" );
       }
    
       public static void Print2()//静态方法
       {
          Console.WriteLine( "Print2 -- static" );
       }
    }
    
    class Program
    {
       static void Main()
       {
          Test t = new Test();                 
          PrintFunction pf;                        
          pf = t.Print1; 
    
          pf += Test.Print2;
          pf += t.Print1;
          pf += Test.Print2;
    
          if ( pf != null )                         
          {
              pf();    
           }                          
          else
           {  
               Console.WriteLine( "Delegate is empty" ); 
           }
       }
    }
    补充:链式委托写法1
                // 申明委托并绑定第一个方法
                TestMulticastDelegate tmd = new TestMulticastDelegate(PrintMessage1);
                // 绑定第二个方法
                tmd += new TestMulticastDelegate(PrintMessage2);
                // 绑定第三个方法
                tmd += new TestMulticastDelegate(PrintMessage3);
                // 调用委托
                tmd();
     
    链式委托写法2
          如上的例子
    链式委托写法3
        TestMulticastDelegate tmd1 = new         TestMulticastDelegate(PrintMessage1);
        TestMulticastDelegate tmd2 = new  TestMulticastDelegate(PrintMessage2);
        TestMulticastDelegate tmd3 = new     TestMulticastDelegate(PrintMessage3);
        // 核心本质:将三个委托串联起来
        TestMulticastDelegate tmd = tmd1 + tmd2 + tmd3;
        tmd.Invoke();
    
    在实际开发中经常使用第二种方法,但是却不能不了解方法三,它是链式委托的本质所在。
    //http://www.cnblogs.com/edisonchou/p/4827578.html 链式委托的讲解

    二、匿名方法

      在委托所持有的方法中,如果某个方法只被使用一次,这种情况下,除了创建委托语法的需要,没有必要创建独立的具名方法。匿名方法应运而生。

      匿名方法是在初始化委托时内联(inline)声明的方法。

    using System;
    
    class Program
    {
      delegate int OtherDel(int InParam);
    
      static void Main()
      {
        OtherDel del = delegate(int x)
                       {
                         return x + 20;
                       };
        Console.WriteLine("{0}", del(5));
        Console.WriteLine("{0}", del(6));
      }
    }

    三、Lambda表达式:匿名方法的另一种形式,更易阅读

      (1)本质:简化语法的”语法糖“;

      (2)要点:

        ①Lambda表达式中的参数列表(参数数量、类型和位置)必须与委托相匹配;

        ②表达式中的参数列表不一定需要包含类型,除非委托有ref或out关键字(此时必须显示声明);

        ③如果没有参数,必须使用一组空的圆括号;

      (3)语法:

    所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。

    参考文章:

    http://www.cnblogs.com/edisonchou/p/3704510.html

    http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html

  • 相关阅读:
    [考试反思]0421省选模拟76:学傻
    [考试反思]0420省选模拟75:安在
    [考试反思]0418省选模拟74:杂枝
    [考试反思]0417省选模拟73:纠结
    [考试反思]0416省选模拟72:停滞
    [考试反思]0415省选模拟71:限制
    [考试反思]0414省选模拟70:阻塞
    [考试反思]0413省选模拟69:遗弃
    [考试反思]0411省选模拟68:毒瘤
    [考试反思]0410省选模拟67:迷惑
  • 原文地址:https://www.cnblogs.com/tiantianle/p/5727498.html
Copyright © 2020-2023  润新知