C# 2.0 匿名方法:
DemoDelegate myDelegate = delegate() { Console.WriteLine("Hiya!!"); };
C# 3.0 Lambda 運算式(更簡潔、函數式風格):
DemoDelegate myDelegate = () => Console.WriteLine("Hiya!!");
參數列表 => 運算式/陳述式。Lambda 是匿名方法的**功能超集**:
Expression<T> 運算式樹(匿名方法無法)SQL 風格語法整合至 C#,稱為 **LINQ (Language Integrated Query)**。
public class CoOrdinate { public int x; public int y; }
List<CoOrdinate> coords = ...;
var filteredCoords = from c in coords
where c.x == 1
select (c.x, c.y);
關鍵字 from、where、select 為查詢運算式,底層轉換為 Lambda + 延伸方法:
where c.x == 1 → coords.Where(c => c.x == 1)
運用匿名型別、延伸方法、隱含型別區域變數等 C# 3.0 特性,整合異質資料源。
新型別 System.Expressions.Expression<T> 讓運算式成為執行期資料:
Expression<DemoDelegate> filter = () => Console.WriteLine("Hiya!!");
記憶體中 Lambda 運算式的樹狀結構表示,程式碼可在執行時**檢查、修改**運算式樹內容。
C# 3.0 新特性讓開發者與架構師工作更輕鬆,保持語言嚴謹與結構清晰。雖仍在發展中,但建立在強大 .NET Framework 基礎上,架構與設計模式值得關注。