首页 > 技术文章 > LAMBDA表达式

huang3 2015-12-25 19:48 原文

一、概念

  1、委托:可以执行一串方法

      委托实例化时需要以方法名称做为参数。

  2、Action和Func是系统自带的泛型委托,有16个输入参数或无参数(共17中情形),Func还带一个返回参数。

  3、对象中This的用法

     This只能在静态类的静态方法中第一个参数上才能加,其他地方都不可以。

       如:public.static void Show(This int iValue){...}

            这时:class.Show(iValue);   ---- 传统写法

                     iValue.Show();        ---- 扩展写法:This是给现有类型注入了当前方法。

二、Lambda表达式:

  1、声明委托

      ShowHandler shower1 = new ShowHandler(Show);

      ShowHandler shower1123 = Show;      //委托的右边除了方法名字都可以省略

  2、给委托建立匿名方法

     ShowHandler shower2 = new ShowHandler

     (

         delegate(int x, int y)      //使用匿名方法:delegate

         {

             Console.WriteLine(Plus(x, y));

         }

     );

  3、把匿名标志改成[=>]符号

     ShowHandler shower3 = new ShowHandler

     (

          (int x, int y) =>          //把匿名函数标志换成:=>

          {

              Console.WriteLine(Plus(x, y));

              Console.WriteLine(Plus(x, y));

          }

     );

  4、去掉没用的参数类型

     ShowHandler shower4 = new ShowHandler

     (

         (x, y) =>                //由于参数的数据类型在委托中已经确定,所以省略掉参数类型

         {

             Console.WriteLine(Plus(x, y));

             Console.WriteLine(Plus(x, y));

         }

     );

  5、方法体内只有一个语句时省略方法体的大括弧

     ShowHandler shower5 = new ShowHandler

     (

         (x, y) => Console.WriteLine(Plus(x, y)) //终极模式

     );

  6、方法中只有一个参数时参数外面的括弧也可以省略

     Action<int> act = new Action<int>

     (

         x => Console.WriteLine(x)         //终极模式

     );

  7、如果方法中无参数或一个以上时必须使用括弧

     Action aaaa = new Action

     (

         () => Console.WriteLine(123)      //终极模式

     );

 

三、无(有)返回值的委托:

  1、无返回值的委托:系统给出了Action,最多可以带16个参数,共17中方法。

      Action<int> act = new Action<int>

      (

          x => Console.WriteLine(x)

      );

     写成终极模式:Action<int> act = x => Console.WriteLine(x);

  2、有返回值的委托:系统给出了Func,最多可以带16个输入参数和外加一个输出参数,也是有17中方法。

     Func<int, int, int> tFunc = new Func<int, int, int>    //Func的类型中最后一个必须是返回类型

     (

          (x, y) => x + y

     );

     写成终极模式:Func<int, int, int> tFunc = (x, y) => x + y;

    

四、Linq扩展:

  1、普通方法

     List<People> pListCommon = new List<People>();

     foreach (People p in peopleList)

     {

         if (p.Age > 30)    //用遍历集合的方式判断出其中大于年龄大于30的人

         {

             pListCommon.Add(p);   找出来的人放入新的集合中

         }

     }

  2、使用Linq标准方法

     IEnumerable<People> pEnumrable = from p in peopleList

                                      where p.Age > 30     //Linq和SQL很像似,但语法顺序有明显变化

                                      select p;

     **复杂一点Linq举例

       var oResult = from i in miniList   

                     join b in bigList     //两个集合关联查询:INNER JOIN(默认不写INNER),LEFT JOIN,RIGHT JOIN

                     on sting.Format("{0}",i*10) equals on sting.Format("{0}",b)

                     where i>5

                     select new

                     {

                         mini = i,

                         big =b

                     }

   3、Linq加Lambda表达式

      IEnumerable<People> iResult1 = Enumerable.Where(peopleList, p => p.Age > 30);

   4、Linq扩展:Where方法的第一个参数有This的标示就可以简写为:

      IEnumerable<People> iResult2 = peopleList.Where(p => p.Age > 30);

    

推荐阅读