C#2.0介紹了一個新特性--匿名方法,允許開發者在線(inline)聲明自己的函數代碼而無須使用委托函數(delegate function)。C#3.0中提供了一個新特性--Lambda表達式,它提供了完成相同目標的更加簡潔的格式。讓我們在討論Lambda表達式以前仔細研究一下匿名方法。
匿名方法
假設你需要創建一個按鈕,當點擊它的時候更新ListBox裡的內容。在C#1.0和1.1裡,你要這樣做:
public MyForm()
{
listBox = new ListBox(...);
textBox = new TextBox(...);
addButton = new Button(...);
addButton.Click += new EventHandler(AddClick);
}
void AddClick(object sender, EventArgs e)
{
listBox.Items.Add(textBox.Text);
} 在C#2.0裡,你需要這樣做:
public MyForm()
{
listBox = new ListBox(...);
textBox = new TextBox(...);
addButton = new Button(...);
addButton.Click += delegate
{
listBox.Items.Add(textBox.Text);
}; 就像你看到的一樣,你不必要特別的聲明一個新方法來將它連接到一個事件上。你可以在C#2.0裡使用匿名方法來完成同樣的工作。C#3.0裡介紹了一種更加簡單的格式,Lambda表達式,你可以直接使用"=>"來書寫你的表達式列表,後面跟上一個表達式或者語句塊。
Lambda表達式中的參數
Lambda表達式中的參數可以是顯式或者隱式類型的。在一個顯式類型參數列表裡,每個表達式的類型是顯式指定的。在一個隱式類型參數列表裡,類型是通過上下文推斷出來的:
(int x) => x + 1 // 顯式類型參數
(y,z) => return y * z; // 隱式類型參數 Lambda演算實例
下面的例子給出了兩種不同的方法來打印出一個list中長度為偶數的字符串。第一種方法AnonMethod使用了匿名方法,第二種LambdaExample則是通過Lambda演算實現:
// Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.XML.XLinq;
using System.Data.DLinq;
namespace LambdaExample
{
public delegate bool KeyValueFilter<K, V>(K key, V value);
static class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("AA");
list.Add("ABC");
list.Add("DEFG");
list.Add("XYZ");
Console.WriteLine("Through Anonymous method");
AnonMethod(list);
Console.WriteLine("Through Lambda expression");
LambdaExample(list);
Dictionary<string, int> varClothes= new Dictionary<string,int>();
varClothes.Add("Jeans", 20);
varClothes.Add("Shirts", 15);
varClothes.Add("Pajamas", 9);
varClothes.Add("Shoes", 9);
var ClothesListShortage = varClothes.FilterBy((string name,
int count) => name == "Shoes" && count < 10);
// example of multiple parameters
if(ClothesListShortage.Count > 0)
Console.WriteLine("We are short of shoes");
Console.ReadLine();
}
static void AnonMethod(List<string> list)
{
List<string> evenNumbers = list.FindAll(delegate(string i)
{ return (i.Length % 2) == 0; });
foreach (string evenNumber in evenNumbers)
{
Console.WriteLine(evenNumber);
}
}
static void LambdaExample(List<string> list)
{
var evenNumbers = list.FindAll(i =>(i.Length % 2) == 0); // example of single parameter
foreach(string i in evenNumbers)
{
Console.WriteLine(i);
}
}
}
public static class Extensions
{
public static Dictionary<K, V> FilterBy<K, V>
(this Dictionary<K, V> items, KeyValueFilter<K, V> filter)
{
var result = new Dictionary<K, V>();
foreach(KeyValuePair<K, V> element in items)
{
if (filter(element.Key, element.Value))
result.Add(element.Key, element.Value);
}
return result;
}
}
} 如果你安裝了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 中間語言結果顯示
雙擊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);