• Lambda表达式拆解


    1. static string GetResult(Func<string, string> f)
              {
                  string s = "ZAZA";
                  return f(s);
              }
              /// <summary>
              /// Main
              /// </summary>
              /// <param name="args"></param>
              static void Main(string[] args)
              {
                  Console.WriteLine(GetResult(GetStr));
              }
              /// <summary>
              /// 获取字符串
              /// </summary>
              /// <param name="str"></param>
              /// <returns></returns>
              static string GetStr(string str)
              {
                  return str + "is dont";
              }

      获取字符串转换成匿名方法

    2. /// <summary>
              /// 获取结果
              /// </summary>
              /// <param name="f"></param>
              /// <returns></returns>
              static string GetResult(Func<string, string> f)
              {
                  string s = "ZAZA";
                  return f(s);
              }
              /// <summary>
              /// Main
              /// </summary>
              /// <param name="args"></param>
              static void Main(string[] args)
              {
                  Console.WriteLine(
                      GetResult(
                                   delegate (string str){
                                      return str + "is dont";
                                   }
                               )
                   );
              }  

      转换箭头函数

    3. /// <summary>
              /// 获取结果
              /// </summary>
              /// <param name="f"></param>
              /// <returns></returns>
              static string GetResult(Func<string, string> f)
              {
                  string s = "ZAZA";
                  return f(s);
              }
              /// <summary>
              /// Main
              /// </summary>
              /// <param name="args"></param>
              static void Main(string[] args)
              {
                  Console.WriteLine(
                      GetResult(
                                   (string str)=>{
                                      return str + "is dont";
                                   }
                               )
                   );
              }       

      简写

    4. /// <summary>
              /// 获取结果
              /// </summary>
              /// <param name="f"></param>
              /// <returns></returns>
              static string GetResult(Func<string, string> f)
              {
                  string s = "ZAZA";
                  return f(s);
              }
              /// <summary>
              /// Main
              /// </summary>
              /// <param name="args"></param>
              static void Main(string[] args)
              {
                  Console.WriteLine(
                      GetResult(str=> str + "is dont")
                   );
              }  
    5. 参数列表 => 语句或语句块

        例子:

    //无返回值 无参数
                //void getstring(){Console.WriteLine("xxx")}
                Action action = () => Console.WriteLine("xxx");
                action();
    
                //无返回值 一个参数  void getstring(string str){Console.WriteLine(str)}
                Action<string> actionpram =s=> Console.WriteLine(s);
                actionpram("SayWhat?");
    
                //无返回值 多个参数
                //void getstring(string s,string d){Console.WriteLine($"第一个参数{s},第二个参数{d}")}
                Action<string,string> actionprams = (s,d) => Console.WriteLine($"第一个参数{s},第二个参数{d}");
                actionprams("what up","say Hi!");
    
    
    
                //有返回值 无参数
                //DateTime getDate(){return DateTime.Now;}
                Func<DateTime> fnc = () => DateTime.Now;
    
                //有返回值 一个参数
                // DateTime getDate(string time){return Convert.ToDateTime(s);}
                Func<string,DateTime> fn = s =>Convert.ToDateTime(s);
    
                //有返回值 多个参数
                // string getString(int a,int b){return (a+b).ToString();} 
                Func<int,int, string> fncc = (i1,i2) => (i1+i2).ToString();
  • 相关阅读:
    MySQL性能调优——索引详解与索引的优化
    Linux命令之文件搜索
    MySQL中的行级锁,表级锁,页级锁
    MySQL存储引擎
    Linux软链接和硬链接
    linux学习笔记
    在浏览器中输入一个网址后,发生了什么?
    二叉排序树
    有序表查找
    为view设置虚线边框
  • 原文地址:https://www.cnblogs.com/Zingu/p/14595444.html
Copyright © 2020-2023  润新知