较之前一个版本,对于C# 3.x和VB 9来说,LINQ是最具吸引力的。基本上很多的新的特性都是围绕着LINQ的实现来设计的。借助Extension Method,我们可以为LINQ定义一系列的Operator。通过Lambda Expression我们可以为LINQ编写更加简洁的查询。我们可以说这些新的特性成就了LINQ,也可以说这些新特性就是为了实现LINQ而产生,但是我们应该明白,对于这些新引入的特性,LINQ并非他们唯一的用武之地,在一般的编程中,我们也可以使用它们。

继上一章,介绍Extension Method之后,我们接着来介绍另一个重要的特性:Lambda Expression。在前面的两篇文章中,我一再在强调这样的一个概念:C# 3.x新引入的这些特性仅仅反映在Programming Language和相应的Compiler层面。通过编译生成的Assembly的IL和原来并没有本质的改变。从这个意义上讲,所有的这些其实是编译器给我们玩得障眼法而已。Lambda Expression也不例外, Lambda Expression就是一个Anonymous Delegate,无论是Named Delegate也好、Anonymous Delegate也好,其本质也就是一个Delegate。

接下来,我将通过一个简单的Demonstration,由浅入深地分析Lambda Expression,看看编译器到底会编译生成怎样的额外的Code,他们的IL又是如何。

一、Named Delegate

在上面,我说了Lambda Expression本质上就是一个Delegate,我们先不直接来介绍Lambda Expression, 我们先来看看我们最为熟悉的Delegate的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Artech.LambdaExpression
{
    class Program
    {
        static void Main()
        {
            _namedMethodDelegate = new Function<int, bool>(SomeMethod);
            Function<int, bool> function1 = _namedMethodDelegate;
            function1(20);     

        }
        private static Function<int, bool> _namedMethodDelegate;

        private static bool SomeMethod(int args)
        {
            return (args > 0);
        }

    }

    delegate TResult Function<TArgs, TResult>(TArgs args);

}

上面的例子很简单,先定一个Generic Delegate :Function。在Program Class中定义一个Static的Function字段_namedMethodDelegate和与之对应的Method:SomeMethod,判断输入的数字是否大于零。在Main中实例化_namedMethodDelegate,并调用它。

我们通过IL Disassembler这个Utility来看看Main方法的IL代码。为了让对IL Instruction不是很了解的读者更加容易地理解整个执行过程,我加了简单注释。对于那些希望进一步了解整个MSIL Instruction列表的读者,可以参考:MSIL Instruction Table。

.method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       34 (0x22)
  .maxstack  3
  .locals init ([0] class Artech.LambdaExpression.Function`2<int32,bool> function1)//Initialize function1
  IL_0000:  nop
  IL_0001:  ldnull
  IL_0002:  ldftn      bool Artech.LambdaExpression.Program::SomeMethod(int32)//Pushes the method pointer referenced by SomeMethod. 
  IL_0008:  newobj     instance void class Artech.LambdaExpression.Function`2<int32,bool>::.ctor(object,
                                                                                                 native int)//Initializer a Artech.LambdaExpression.Function delegate instance.
  IL_000d:  stsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::_namedMethodDelegate//Stores a static field: _namedMethodDelegate
  IL_0012:  ldsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::_namedMethodDelegate//Pushes the static field(_namedMethodDelegate)  of an object Static .
  IL_0017:  stloc.0   //Pop the first local variable 
  IL_0018:  ldloc.0   //Pushes the first local variable 
  IL_0019:  ldc.i4.s   20 //Pushes specified 8-bit value (20) as 32-bit 
  IL_001b:  callvirt   instance !1 class Artech.LambdaExpression.Function`2<int32,bool>::Invoke(!0)//Calls virtual method of delegate instance. 
  IL_0020:  pop
  IL_0021:  ret
} // end of method Program::Main

对于Delegate,我无须再作深入的介绍,相信大家早已了如指掌。在这里需要着重提出是,上面介绍的内容将是后续部分的基础,通过后面的对Anonymous Method和Lambda expression介绍,你会发现它们生成的代码结构和上面的是非常相似的。

二、  Anonymous Method Delegate

Anonymous Method是C# 2.0引入的一个非常好用的功能。通过Anonymous Method,我们可以Delegate的实现直接以Inline的方式放入Delegate对象使用的位置,而无须再繁琐地创建一个Delegate,并通过定义在某个Class中具有相同申明的Method来事例化这个Delegate Instance,最后才将这个delegate instance传入需要调用的Method。

我们现在通过Anonymous Method来简化上面的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Artech.LambdaExpression
{
    class Program
    {
        static void Main()
        {
            Function<int, bool> function2 = delegate(int args)
            {
                return args > 0;
            };
            function2(20);   
        }
    }
    delegate TResult Function<TArgs, TResult>(TArgs args);
}

我们通过Reflector分析编译生成的Assembly,我们发现它具有下面的结构。进一步分析Program Class,我们发现它多了两个额外的Static成员:<>9__CachedAnonymousMethodDelegate1和<Main>b__0。这是编译器的功劳。

下面分别是<>9__CachedAnonymousMethodDelegate1和<Main>b__0的定义:

[CompilerGenerated]

private static Function<int, bool> <>9__CachedAnonymousMethodDelegate1;

[CompilerGenerated]

private static bool <Main>b__0(int args)

{

    return (args > 0);

}

是不是我我们上面一节定义的_namedMethodDelegate和SomeMethod这个两个静态成员一样?

我们进一步分析Main Method的IL。

.method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       43 (0x2b)
  .maxstack  3
  .locals init ([0] class Artech.LambdaExpression.Function`2<int32,bool> function2)
  IL_0000:  nop
  IL_0001:  ldsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::'<>9__CachedAnonymousMethodDelegate1'
  IL_0006:  brtrue.s   IL_001b
  IL_0008:  ldnull
  IL_0009:  ldftn      bool Artech.LambdaExpression.Program::'<Main>b__0'(int32)
  IL_000f:  newobj     instance void class Artech.LambdaExpression.Function`2<int32,bool>::.ctor(object,
                                                                                                 native int)
  IL_0014:  stsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::'<>9__CachedAnonymousMethodDelegate1'
  IL_0019:  br.s       IL_001b
  IL_001b:  ldsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::'<>9__CachedAnonymousMethodDelegate1'
  IL_0020:  stloc.0
  IL_0021:  ldloc.0
  IL_0022:  ldc.i4.s   20
  IL_0024:  callvirt   instance !1 class Artech.LambdaExpression.Function`2<int32,bool>::Invoke(!0)
  IL_0029:  pop
  IL_002a:  ret
} // end of method Program::Main

和我们上面一节的IL对比,是否出奇地相似。所用我们可以说,我们在第一节中Named Delegate和Anonymous Method Delegate是等效的。

接下来我们通过Lambda Expression实现上面的功能。

三、 Lambda Expression

下面是通过Lambda Expression实现上面相同功能的Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Artech.LambdaExpression
{
    class Program
    {
        static void Main()
        {
            Function<int, bool> function3 = x => x > 0;
            function3(20);
        }
    }

    delegate TResult Function<TArgs, TResult>(TArgs args);

}

我们通过Reflector分析编译生成的Assembly,我们发现它和通过Anonymous Method Delegate实现的完全一样:Program Class,我们发现它多了两个额外的Static成员,它们的名称都完全一样:<>9__CachedAnonymousMethodDelegate1和<Main>b__0。

这两个Static Member:<>9__CachedAnonymousMethodDelegate1和<Main>b__0的定义也于我们通过Anonymous Method Delegate实现时一模一样:

[CompilerGenerated]

private static Function<int, bool> <>9__CachedAnonymousMethodDelegate1;

[CompilerGenerated]

private static bool <Main>b__0(int args)

{

    return (args > 0);

}

我们进一步来看看Main Method的IL。

.method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       43 (0x2b)
  .maxstack  3
  .locals init ([0] class Artech.LambdaExpression.Function`2<int32,bool> function3)
  IL_0000:  nop
  IL_0001:  ldsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::'<>9__CachedAnonymousMethodDelegate1'
  IL_0006:  brtrue.s   IL_001b
  IL_0008:  ldnull
  IL_0009:  ldftn      bool Artech.LambdaExpression.Program::'<Main>b__0'(int32)
  IL_000f:  newobj     instance void class Artech.LambdaExpression.Function`2<int32,bool>::.ctor(object,
                                                                                                 native int)
  IL_0014:  stsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::'<>9__CachedAnonymousMethodDelegate1'
  IL_0019:  br.s       IL_001b
  IL_001b:  ldsfld     class Artech.LambdaExpression.Function`2<int32,bool> Artech.LambdaExpression.Program::'<>9__CachedAnonymousMethodDelegate1'
  IL_0020:  stloc.0
  IL_0021:  ldloc.0
  IL_0022:  ldc.i4.s   20
  IL_0024:  callvirt   instance !1 class Artech.LambdaExpression.Function`2<int32,bool>::Invoke(!0)
  IL_0029:  pop
  IL_002a:  ret
} // end of method Program::Main

和上面通过

Anonymous Method Delegate实现的时候完全是一样的。

四、Conclusion

现在我们可以得出结论了,Lambda Expression本质上是一个Anonymous Method Delegate,这个Delegate的匿名性仅仅针对Programming language而言,编译器会为它生成一个Named delegate和一个它指向的Method。这个两个额外生成的对象作为使用Anonymous Method Delegate对应的Class的Static Method而存在。从本质上讲和一般的Delegate并没有本质的区别。所以上面我们分别通过Named delegate、Anonymous method delegate和Lambda Expression实现的3个方式是等效的。

C# 3.x相关内容:
[原创]深入理解C# 3.x的新特性(1):Anonymous Type
[原创]深入理解C# 3.x的新特性(2):Extension Method - Part I
[原创]深入理解C# 3.x的新特性(2):Extension Method - Part II
[原创]深入理解C# 3.x的新特性(3):从Delegate、Anonymous Method到Lambda Expression
[原创]深入理解C# 3.x的新特性(4):Automatically Implemented Property
[原创]深入理解C# 3.x的新特性(5):Object Initializer 和 Collection Initializer

[原创]深入理解C# 3.x的新特性(3):从Delegate、Anonymous Method到Lambda Expression相关推荐

  1. 深入理解C# 3.x的新特性(2):Extension Method[下篇]

    四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...

  2. 深入理解C# 3.x的新特性(5):Object Initializer 和 Collection Initializer

    深入理解C# 3.x的新特性系列自开篇以后,已经有两个月了.在前面的章节中,我们先后深入讨论了C# 3.x新引入的一些列新特性:Anomynous Type.Extension Method.Lamb ...

  3. 《深入理解C++11:C++ 11新特性解析与应用》——1.3 C++11特性的分类

    1.3 C++11特性的分类 从设计目标上说,能够让各个特性协同工作是设计C++11/0x中最为关键的部分.委员会总希望通过特性协作取得整体大于个体的效果,但这也是语言设计过程中最困难的一点.因此相比 ...

  4. 《深入理解C++11:C++ 11新特性解析与应用》——导读

    前 言 为什么要写这本书 相比其他语言的频繁更新,C++语言标准已经有十多年没有真正更新过了.而上一次标准制定,正是面向对象概念开始盛行的时候.较之基于过程的编程语言,基于面向对象.泛型编程等概念的C ...

  5. 《深入理解C++11:C++ 11新特性解析与应用》——2.4 宏__cplusplus

    2.4 宏__cplusplus 类别:部分人 在C与C++混合编写的代码中,我们常常会在头文件里看到如下的声明: #ifdef __cplusplus extern "C" { ...

  6. 《深入理解C++11:C++ 11新特性解析与应用》——3.2 委派构造函数

    3.2 委派构造函数 类别:类作者 与继承构造函数类似的,委派构造函数也是C++11中对C++的构造函数的一项改进,其目的也是为了减少程序员书写构造函数的时间.通过委派其他构造函数,多构造函数的类编写 ...

  7. C++ 11三个新特性的简单使用 - std::function、lambda 表达式、智能指针

    使用Dev C++:先设置Dev C++ 支持 C++ 11:见此文后部: https://blog.csdn.net/bcbobo21cn/article/details/111466179 1 s ...

  8. c++11新特性的使用---可变模板参数、lambda、type_traits、optional综合使用

    通过惰性求值类的实现综合采用了可变模板参数.lambda.type_traits.optional进行实现 该类功能是:当初始化某个对象时,该对象引用了一个大对象,这个对象的创建需要较长的时间,同时有 ...

  9. java lambda表达式详解_Java8新特性:Lambda表达式详解

    在 Java 版本的历次更新迭代中,Java8 是一个特殊的存在,与以往的版本升级不同.我们对 Java8 似乎抱有更大的期待,因为它是 Java5 之后最重要的一次升级,提供了十多个新特性,其中 L ...

最新文章

  1. 一劳永逸,iOS 自定义 ActionSheet 封装流程
  2. 生态伙伴 | xiaopiu入驻飞书,助你高效创作,从原型到文档一站搞定!
  3. PHP CURL 哈哈哈哈哈记录一下
  4. 了解ReactOS调试
  5. C语言//注释使下一行代码失效
  6. linux mktime函数会受当前环境变量设置的时区影响
  7. 长大了Java! 提出Java桌面版
  8. Response.Redirect奇怪现象
  9. 行业最大!黑鲨5系列将搭载5320mm²双VC散热:冰封骁龙8
  10. centerpython_centeros下安装python3
  11. HDU4027 (线段树/修改区间,询问区间和)
  12. 使用cdn和npm引入的区别_带你体验 Vue2 和 Vue3 开发组件有什么区别
  13. 简单实用:浏览器嗅探扩展抓取下载音乐和视频
  14. TiledMap快捷键
  15. Windows 7精简版(2019)
  16. 那些设计出来就不希望别人看懂的C代码——IOCCC国际模糊C代码大赛
  17. FFmpeg MP4文件提取音频文件
  18. Docker下搭建XSS挑战之旅靶场
  19. 41 ItemsWidget之QListWidget
  20. simple_bypass

热门文章

  1. Linux环境中MySQL主从同步--添加新的从库
  2. 看博客学学Android(二十一)
  3. InstallAnywhere制作出来的安装包在64位机上无法卸载
  4. 堪比“脏牛”!“脏管道” 漏洞可获得Linux 所有主流发行版本的root权限
  5. 德国政府证实警方使用了 Pegasus 间谍软件
  6. 速修复!Netgear交换机曝3个严重的认证绕过漏洞
  7. Linux 内核漏洞暴露栈内存,造成数据泄露
  8. 这个 WebKit 漏洞助力 Pwn2Own 冠军斩获5.5万美元赏金(详细分析)
  9. http状态码竟然可以这样记
  10. 统计学习方法---牛顿法