zoukankan      html  css  js  c++  java
  • 扩展方法、委托和Lambda

    举例演化Lambda

    string[] names ={"Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David"};

    //问题:从数组names中筛选字符长度为5的成员

    //先声明一个返回类型为bool的静态函数

    public static bool IsLengthFive(string s)

    {

      return s.length == 5;

    }

    //Enumerable中的Where方法是扩展方法,要求返回类型为bool参数类型为String的委托实例

    //a01为where方法提供命名函数

    var nameA = names.where(IsLengthFive);

    //a02为where方法提供匿名函数

    var nameB = names.where(delegate(string s){return s.length == 5;});

    //a03静态类中静态方法的调用

    IEnumerable<string> nameC = Enumerable.where(names,delegate(string a){return a.length == 5});

    //Lambda演化

    ///b01为扩展方法传递匿名方法(委托匿名实例)=>匿名函数使用Lambda表示式来表示(为扩展方法传Lambda)

    var query = name.where(s=>s.length == 5)

             .orderBy(s=>s)

             .select(s=>s.ToLower())

    !!!  Lambda表达式会被转化成匿名函数,匿名函数会转化为委托(匿名函数是因委托而存在的)

    /////c01 Linq(查询表达式)//查询操作符,表达形式可以参照数据库的查询语句

     var queryB = from b in names

           where b.length == 5

          orderBy b

          select b.ToLower();

  • 相关阅读:
    c++中的数据类型
    C语言堆栈入门——堆和栈的区别
    用(*.frm *.MYD *.MYI)文件恢复MySql数据库
    GridView总结二:GridView自带编辑删除更新
    GridView总结一:GridView自带分页及与DropDownList结合使用
    对自己寒假的安排
    Python3 File
    Python3输入输出
    Python模块
    Python3数据结构
  • 原文地址:https://www.cnblogs.com/fllowerqq/p/8783984.html
Copyright © 2011-2022 走看看