WEB开发网
开发学院软件开发C语言 关于Lambda表达式 阅读

关于Lambda表达式

 2009-03-31 08:21:48 来源:WEB开发网   
核心提示: .method private hidebysig static void Main(string[] args) cil managed{ .entrypoint // 代码大小 58 (0x3a) .maxstack 3 .locals init ([0] int32 a, [1] i

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // 代码大小       58 (0x3a)
  .maxstack  3
  .locals init ([0] int32 a,
           [1] int32 b,
           [2] class ConsoleApplication3.Calculate result)
  IL_0000:  nop
  IL_0001:  ldc.i4.3
  IL_0002:  stloc.0
  IL_0003:  ldc.i4.4
  IL_0004:  stloc.1
  IL_0005:  ldsfld     class ConsoleApplication3.Calculate ConsoleApplication3.Program::'CS$<>9__CachedAnonymousMethodDelegate1'
  IL_000a:  brtrue.s   IL_001f
  IL_000c:  ldnull
  IL_000d:  ldftn      int32 ConsoleApplication3.Program::'<Main>b__0'(int32,
                                                                       int32)
  IL_0013:  newobj     instance void ConsoleApplication3.Calculate::.ctor(object,
                                                                          native int)
  IL_0018:  stsfld     class ConsoleApplication3.Calculate ConsoleApplication3.Program::'CS$<>9__CachedAnonymousMethodDelegate1'
  IL_001d:  br.s       IL_001f
  IL_001f:  ldsfld     class ConsoleApplication3.Calculate ConsoleApplication3.Program::'CS$<>9__CachedAnonymousMethodDelegate1'
  IL_0024:  stloc.2
  IL_0025:  ldloc.2
  IL_0026:  ldloc.0
  IL_0027:  ldloc.1
  IL_0028:  callvirt   instance int32 ConsoleApplication3.Calculate::Invoke(int32,
                                                                            int32)
  IL_002d:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0032:  nop
  IL_0033:  call       int32 [mscorlib]System.Console::Read()
  IL_0038:  pop
  IL_0039:  ret
} // end of method Program::Main

可以看出IL_0005: 到IL_001f 这段代码是初始化编译器为我们生成的委托

下面几行IL指令可以看出来是为我们初始化委托

IL_000c: ldnull     //声明一个空的函数指针

IL_000d: ldftn   //加载方法指针 指向 '<Main>b__0'(int32,int32) 函数

IL_0013: newobj //实例化委托

IL_0018: stsfld //保存

透过IL代码 我们看到了Lambda表达式的的正真实现方法….

看到了C#语言没有什么改变 只是编译器为我们完成了很多东西

上一页  1 2 3 

Tags:关于 Lambda 表达式

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接