zoukankan      html  css  js  c++  java
  • 函数~匿名方法~lambda的逐渐过渡思想

    前提:基于委托实现

    (1)使用函数名称

    delegate void Printer(string s);//(1)申明委托
    static void Main(string[] args) { //(3)委托与命名方法关联 p = new Printer(DoWork);
    p(
    "The delegate using the named method is called.");//(4)调用委托
    Console.ReadKey(); }
    static void DoWork(string k)//(2)根据委托创建具有相同签名的方法 { Console.WriteLine(k); }


    对于一个方法来说方法名是什么是无关紧要的,重要的是参数所以可以使用匿名函数(方法)

    (2)使用匿名函数

    delegate void Printer(string s);//(1)申明委托
            static void Main(string[] args)
            {
                //(2)委托与匿名方法关联
                Printer p = delegate (string st)
                  {
                      Console.WriteLine(st);
                  };
                p("The delegate using the anonymous method is called.");//调用委托
    
                Console.ReadKey();
            }
           

    (3)还有比匿名方法更加高级的方法

    delegate void TestDelegate(string s);//(1)申明委托
    static void Main(string[] args) { //Lambda语句用于创建委托 TestDelegate myDel = (n) => //委托与具体的方法相关连
    {
        string s = n + " " + "World"; Console.WriteLine(s);

    };
    myDel(
    "Hello");//+(3)调用委托 }

    //myDel :是委托对象 n:是与委托相对应的参数 (n)可以直接简写成n



  • 相关阅读:
    【结构型】Proxy模式
    【结构型】Flyweight模式
    【结构型】Facade模式
    【结构型】Decorate模式
    【结构型】Composite模式
    适配器模式 -- 大话设计模式
    状态模式 -- 大话设计模式
    抽象工厂模式 -- 大话设计模式
    建造者模式 -- 大话设计模式
    观察者模式 -- 大话设计模式
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/6548975.html
Copyright © 2011-2022 走看看