c# 3.0新特性体验之lambda表达式_c#教程(2)

2008-02-23 05:45:34来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


  中间语言结果显示

  打开ILDASM我们来查看一下程式,您将看到如图1所示的内容:


图1

  双击AnonMethod函数您将看到C#编译器产生的中间语言代码:

.method private hidebysig static void AnonMethod(class
[mscorlib]System.Collections.Generic.List`1<string> list)
cil managed
{
 // Code size 96 (0x60)
 .maxstack 4
 .locals init ([0] class [mscorlib]System.Collections.Generic.List
 `1<string> evenNumbers,
 [1] string evenNumber,
 [2] valuetype [mscorlib]System.Collections.Generic.List
 `1/Enumerator<string> CSCODE_REPLACEMENT 000,
 [3] bool CSCODE_REPLACEMENT 001)
 IL_0000: nop
 IL_0001: ldarg.0
 IL_0002: ldsfld class [mscorlib]System.Predicate
 `1<string> LambdaExample.Program::
 `<>9__CachedAnonymousMethodDelegate1
 IL_0007: brtrue.s IL_001c
 IL_0009: ldnull
 IL_000a: ldftn bool LambdaExample.Program::
 `<AnonMethod>b__0(string)
 IL_0010: newobj instance void class [mscorlib]System.Predicate
 `1<string>::.ctor(object, native int)
 IL_0015: stsfld class [mscorlib]System.Predicate`1<string>
 LambdaExample.Program::
 `<>9__CachedAnonymousMethodDelegate1
 IL_001a: br.s IL_001c
 IL_001c: ldsfld class [mscorlib]System.Predicate`1<string>
 LambdaExample.Program::<>
 9__CachedAnonymousMethodDelegate1
 IL_0021: callvirt instance class [mscorlib]System.Collections.
 Generic.List`1<!0> class [mscorlib]System.
 Collections.Generic.List`1<string>::
 FindAll(class [mscorlib]System.Predicate`1<!0>)
 IL_0026: stloc.0
 IL_0027: nop
 IL_0028: ldloc.0
 IL_0029: callvirt instance valuetype [mscorlib]System.Collections.
 Generic.List`1/Enumerator<!0> class
 [mscorlib]System.Collections.Generic.List`1
 <string>::GetEnumerator()
 IL_002e: stloc.2
 .try
 {
  IL_002f: br.s IL_0042
  IL_0031: ldloca.s CSCODE_REPLACEMENT 000
  IL_0033: call instance !0 valuetype [mscorlib]System.
  Collections.Generic.List`1/Enumerator
  <string>::get_Current()
  IL_0038: stloc.1
  IL_0039: nop
  IL_003a: ldloc.1
  IL_003b: call void [mscorlib]System.Console::
  WriteLine(string)
  IL_0040: nop
  IL_0041: nop
  IL_0042: ldloca.s CSCODE_REPLACEMENT 000
  IL_0044: call instance bool valuetype [mscorlib]System.
  Collections.Generic.List`1/Enumerator
  <string>::MoveNext()
  IL_0049: stloc.3
  IL_004a: ldloc.3
  IL_004b: brtrue.s IL_0031
  IL_004d: leave.s IL_005e
 } // end .try
 finally
 {
  IL_004f: ldloca.s CSCODE_REPLACEMENT 000
  IL_0051: constrained. valuetype [mscorlib]System.Collections.
  Generic.List`1/Enumerator<string>
  IL_0057: callvirt instance void [mscorlib]System.
  IDisposable::Dispose()
  IL_005c: nop
  IL_005d: endfinally
 } // end handler
 IL_005e: nop
 IL_005f: ret
} // end of method Program::AnonMethod

  这里我们能够看到,实际上匿名方法和lambda表达式生成了相同的中间代码,并且他们的执行也是类似的。

  多参数的Lambda表达式

  Lambda表达式能够带上多个参数,比如您能够声明一个Dictionary类型:

Clothing Type Count
Shirts 15
Jeans 12
Shoes 9
Pajamas 9

  假如您有一个匿名方法(FilterBy)来通过键和值来过滤字典,按么您能够传递多个参数给lambda表达式来调用这个匿名方法。附带的代码完成了这个FilterBy的功能:

var ClothesListShortage = clothesList.FilterBy((string name, int count)
=> name == "Shoes" && count < 10);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: c#中除去任何在html元素中标记_c#应用

下一篇: 使用foreach来读取记录_c#应用