本筆記摘抄自:https://www.cnblogs.com/leslies2/archive/2012/03/22/2389318.html,記錄一下學習程序以備后續查用,
一、Lambda 的意義
在Framework 2.0 以前,宣告委托的唯一方法是通過方法命名,從Framework 2.0 起,系統開始支持匿名方法,通過匿名方法,可以直接把一段代碼系結
給事件,因此減少了實體化委托所需的編碼系統開銷,而在 Framework 3.0 開始,Lambda運算式開始逐漸取代了匿名方法,作為撰寫行內代碼的首選方式,
總體來說,Lambda 運算式的作用是為了使用更簡單的方式來撰寫匿名方法,徹底簡化委托的使用方式,
二、回顧匿名方法的使用
匿名方法的使用在C#委托與事件學習筆記中有簡單介紹過,在此回顧一下:
static void Main(string[] args) { #region 事件的使用及方法系結 PersonManager personManager = new PersonManager(); //系結事件處理方法方式一 personManager.MyEvent += new MyDelegate(GetName); //系結事件處理方法方式二 personManager.MyEvent += GetName; //系結事件處理方法方式三(匿名方法) personManager.MyEvent += delegate (string name) { Console.WriteLine("My name is " + name); }; personManager.Execute("Atomy"); Console.Read(); #endregion }View Code
總是使用 delegate(){......} 的方式建立匿名方法,令人不禁感覺郁悶,于是從Framework 3.0起,Lambda運算式開始出現,
三、簡單介紹泛型委托
在介紹Lambda運算式前,先介紹一下常用的幾個泛型委托,
3.1 泛型委托 Predicate<T>
早在Framework 2.0的時候,微軟就為List<T>類添加了Find、FindAll、ForEach等方法用作資料的查找,
public T Find ( Predicate<T> match)
public List<T> FindAll(Predicate<T> match)
在這些方法中存在一個Predicate <T> 運算式,它是一個回傳bool的泛型委托,能接受一個任意型別的物件作為引數,
public delegate bool Predicate<T>(T obj)
在下面例子中,Predicate委托系結了引數為Person類的方法Match作為查詢條件,然后使用FindAll方法查找到合適條件的List<Person>集合,
class Program { #region 泛型委托 Predicate<T> /// <summary> /// Person類 /// </summary> class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public Person(int id, string name, int age) { Id = id; Name = name; Age = age; } } /// <summary> /// 資料源 /// </summary> /// <returns></returns> static List<Person> GetList() { var personList = new List<Person>(); var person = new Person(1, "Hello", 29); personList.Add(person); person = new Person(1, "World", 31); personList.Add(person); return personList; } /// <summary> /// 查詢條件 /// </summary> /// <param name="person"></param> /// <returns></returns> static bool Match(Person person) { return person.Age <= 30; } #endregion static void Main(string[] args) { #region 泛型委托 Predicate<T> List<Person> list = GetList(); //系結查詢條件 Predicate<Person> predicate = new Predicate<Person>(Match); List<Person> result = list.FindAll(predicate); Console.WriteLine($"Person count is : {result.Count}"); Console.Read(); #endregion } }View Code
運行結果如下:

3.2 泛型委托 Action
Action<T> 的使用方式與 Predicate<T> 相似,不同之處在于 Predicate<T> 回傳值為 bool , Action<T> 的回傳值為 void,
Action 支持0~16個引數,可以按需求任意使用,
public delegate void Action()
public delegate void Action<T1> (T1 obj1)
public delegate void Action<T1,T2> (T1 obj1, T2 obj2)
public delegate void Action<T1,T2,T3> (T1 obj1, T2 obj2,T3 obj3)
............
public delegate void Action<T1,T2,T3,......,T16> (T1 obj1, T2 obj2,T3 obj3,......,T16 obj16)
下面代碼演示泛型委托Action:
class Program { #region 泛型委托 Action static void ShowMessage(string message) { Console.WriteLine(message); } #endregion static void Main(string[] args) { #region 泛型委托 Action Action<string> action = ShowMessage; action("Hello World"); Console.ReadKey(); #endregion } }View Code
運行結果如下:

3.3 泛型委托 Func
委托Func與Action相似,同樣支持0~16個引數,不同之處在于Func必須具有回傳值,
public delegate TResult Func<TResult>()
public delegate TResult Func<T1,TResult>(T1 obj1)
public delegate TResult Func<T1,T2,TResult>(T1 obj1,T2 obj2)
public delegate TResult Func<T1,T2,T3,TResult>(T1 obj1,T2 obj2,T3 obj3)
............
public delegate TResult Func<T1,T2,T3,......,T16,TResult>(T1 obj1,T2 obj2,T3 obj3,......,T16 obj16)
下面代碼演示泛型委托Func:
class Program { #region 泛型委托 Func static double Account(double a, bool condition) { if (condition) return a * 1.5; else return a * 2; } #endregion static void Main(string[] args) { #region 泛型委托 Func Func<double, bool, double> func = Account; double result = func(1000, true); Console.WriteLine($"Result is : {result}"); Console.Read(); #endregion } }View Code
運行結果如下:

四、揭開 Lambda 神秘的面紗
Lambda的運算式的撰寫格式如下:x=> x * 1.5
當中 “ => ”是Lambda運算式的運算子,在左邊用作定義一個引數串列,右邊可以操作這些引數,
例子一:先把int x設定1000,通過Action把運算式定義為x=x+500,最后通過Invoke激發委托,
class Program { static void Main(string[] args) { #region Lambda例子一 int x = 1000; Action action = () => x = x + 500; action.Invoke(); Console.WriteLine($"Result is : {x}"); Console.Read(); #endregion } }View Code
運行結果如下:

例子二:通過Action<int>把運算式定義x=x+500,到最后輸入引數1000,得到的結果與例子一相同,
注意,此處Lambda運算式定義的操作使用{ }括弧包括在一起,里面可以包含一系列的操作,
class Program { static void Main(string[] args) { #region Lambda例子二 Action<int> action = (x) => { x = x + 500; Console.WriteLine($"Result is : {x}"); }; action.Invoke(1000); Console.Read(); #endregion } }View Code
運行結果如下:

例子三:定義一個Predicate<int>,當輸入值大約等于1000則回傳true,否則回傳false,與3.1的例子相比,Predicate<T>的系結不需要顯式建立一個方法,
而是直接在Lambda運算式里完成,簡潔方便了不少,
class Program { static void Main(string[] args) { #region Lambda例子三 Predicate<int> predicate = (x) => { if (x >= 1000) return true; else return false; }; bool result = predicate.Invoke(500); Console.WriteLine($"Result={result}"); Console.Read(); #endregion } }View Code
運行結果如下:

例子四:在計算商品的價格時,當商品重量超過30kg則打9折,其他按原價處理,此時可以使用Func<double,int,double>,引數1為商品原價,引數2為商品
重量,最后回傳值為 double 型別,
class Program { static void Main(string[] args) { #region Lambda例子四 Func<double, int, double> func = (price, weight) => { if (weight >= 30) return price * 0.9; else return price; }; double totalPrice = func(200.0, 40); Console.WriteLine($"TotalPrice={totalPrice}"); Console.Read(); #endregion } }View Code
運行結果如下:

例子五:使用Lambda為Button定義Click事件的處理方法,使用Lambda比使用匿名方法更加簡單,
public partial class Main : Form { public Main() { InitializeComponent(); } private void Main_Load(object sender, EventArgs e) { #region Lambda例子五 btnEvent.Click += (obj,arg)=> { MessageBox.Show("Hello World"); }; #endregion } }View Code
運行結果如下:

例子六:此處使用3.1的例子,在List<Person>的FindAll方法中直接使用Lambda運算式,相比之下,使用Lambda運算式,不需要定義Predicate<T>物件,也
不需要顯式設定系結方法,簡化了不工序,
class Program { #region 泛型委托 Predicate<T> /// <summary> /// Person類 /// </summary> class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public Person(int id, string name, int age) { Id = id; Name = name; Age = age; } } /// <summary> /// 資料源 /// </summary> /// <returns></returns> static List<Person> GetList() { var personList = new List<Person>(); var person = new Person(1, "Hello", 29); personList.Add(person); person = new Person(1, "World", 31); personList.Add(person); return personList; } /// <summary> /// 查詢條件 /// </summary> /// <param name="person"></param> /// <returns></returns> static bool Match(Person person) { return person.Age <= 30; } #endregion static void Main(string[] args) { #region Lambda例子六 List<Person> personList = GetList(); //查找年齡少于30年的人 List<Person> result = personList.FindAll((person) => person.Age <= 30); Console.WriteLine("Person count is : " + result.Count); Console.Read(); #endregion } }View Code
運行結果如下:

當在使用LINQ技術的時候,到處都會彌漫著Lambda的身影,此時更能體現Lambda的長處,但LINQ涉及到分部類、分部方法、IEnumerable<T>、迭代器等
多方面的知識,這些已經超出本章的介紹范圍,通過這一節的介紹,希望能夠幫助大家更深入地了解Lambda的使用,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/87791.html
標籤:C#
