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



  • 相关阅读:
    树上莫队 SPOJ COT2
    UVA 11478(差分约束 + 二分)
    可图的度序列判断与构造
    Codeforces Round #306 (Div. 2) 550A Two Substrings
    UVA 11300 分金币
    HDU 2546 饭卡(01 背包)
    hdu 1142 最短路+记忆化
    codeforces ice cave
    codeforces school mark(贪心)
    JavaScript函数
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/6548975.html
Copyright © 2011-2022 走看看