| 加入收藏| 设为首页| 联系我们

首页 站长学习 站长之家 源码下载 建站素材 书籍教程 常用工具
 您现在的位置: 动力中国 >> 网络编程 >> ASP.NET教程 >> 文章正文  
 [组图]深入理解C# 3.0的新特性(3):从Delegate、Anonymous Method到Lambda Expression
 

深入理解C# 3.0的新特性(3):从Delegate、Anonymous Method到Lambda Expression

http://www.domcn.org  文章来源:本站收藏  点击数:

  关键字:深入理解C# 3.0的新特性(3):从Delegate、Anonymous Method到Lambda Expression

;                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 

上一页  [1] [2] [3] [4] 下一页


深入理解C# 3.0的新特性(3):从Delegate、Anonymous Method到Lambda Expression
  • 上一篇文章:

  • 下一篇文章:
  •  热门文章
    普通文章 电子邮件改头换面 四公司畅谈未
    普通文章 PC病毒史上最声名狼藉的八大病
    普通文章 Rails系统中的AJAX开发技术简析
    普通文章 基于ASP.NET AJAX框架实现表单
    普通文章 开发ASP.NET AJAX客户端定制行
    普通文章 用JFreeChart对JSP报表进行增强
    普通文章 SQL Server 2005上的CLR和ADO.
    普通文章 SQL Server 2005的XML支持机制
    普通文章 Firefox中标签式浏览技巧大全
    普通文章 Tomcat中的Session和Cookie大揭
     
     推荐文章
    推荐文章 把Google地图嵌入网页 就是这么
    推荐文章 迅雷搜索候选资源出错的解决
    推荐文章 轻松去除迅雷里的各种广告和资
    推荐文章 突破限制 免费领养到QQ空间五级
    推荐文章 Rational统一过程RUP贴近中小软
    推荐文章 构建自己的轻量级XML DOM分析程
    推荐文章 WPS Office 2007技巧:妙用配置
    推荐文章 Excel 2007:求余数函数实用进阶
    推荐文章 浅谈ASP.NET的Postback
    推荐文章 软件开发中项目需求管理简述
     
     相关文章
    没有相关文章
    设为首页 | 加入收藏 | 广告合作 | 联系站长 | 版权申明 |
    动力中国为网友提供免费学习资料,可用资源,如果您认为我们的相关内容侵害到了您的权利请联系管理员
    Copyright © 2006-2008 domcn.org All Rights Reserved.