程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C# 3.0新特性體驗之Lambda表達式(2)

C# 3.0新特性體驗之Lambda表達式(2)

編輯:關於C語言

如果你安裝了Visual Studio 2005 and LinQ PrevIEw,你可以使用編輯器來 編譯程序。如果沒有的話,可以使用命令行方式:

C:\Program Files\LINQ PrevIEw\Bin\Csc.exe
/reference:"C:\Program Files\LINQ PrevIEw\Bin\System.Data.DLinq.dll"
/reference:C:\Windows\Microsoft.Net\Framework\v2.0.50727 \System.Data.dll
/reference:C:\Windows\Microsoft.Net\Framework\v2.0.50727\System.dll
/reference:"C:\Program Files\LINQ PrevIEw\Bin\System.Query.dll"
/reference:C:\Windows\Microsoft.Net\Framework\v2.0.50727 \System.XML.dll
/reference:"C:\Program Files\LINQ PrevIEw\Bin\System.XML.XLinq.dll"
/target:exe Program.cs

中間語言結果顯示

打開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);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved