国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#Lambda表達式學習日記

2019-11-14 14:12:17
字體:
來源:轉載
供稿:網友

Lambda表達式只是用更簡單的方式來寫匿名方法,徹底簡化了對.NET委托類型的使用。

現在,如果我們要使用泛型 List<> 的 FindAll() 方法,當你從一個集合去提取子集時,可以使用該方法。

// 該方法唯一的參數是一個System.PRedicate<T>類型的泛型委托public List<T> FindAll(Predicate<T> match);// 該委托指向任意以類型參數作為唯一輸入參數并返回bool的方法public delegate bool Predicate<in T>(T obj);

 在調用 FindAll() 時,List<T>中每一項都將傳入Predicate<T> 對象所指向的方法。方法在實現時將執行一些計算,來判斷傳入的數據是否符合標準,并返回 true / false,如果返回 true ,該項被添加到表示自己的List<T>集合中。

現在,我們需要從一個List<int>集合中找到所有偶數,該如何實現呢?

 

1.傳統方法

public class MyDelegateSyntax{public static void Show()    {        Console.WriteLine("fun with lambdas");        List<int> list = new List<int> { 20, 1, 4, 8, 9, 77 };        Predicate<int> callback = new Predicate<int>(IsEvenNumber);        // 傳統方法        List<int> evenList = list.FindAll(callback);        Console.WriteLine();        foreach (int item in evenList)        {            Console.WriteLine(item);        }    }    private static bool IsEvenNumber(int obj) => obj % 2 == 0;}

 

2.匿名方法

public class MyDelegateSyntax{
  public static void Show() { Console.WriteLine("fun with lambdas"); List<int> list = new List<int> { 20, 1, 4, 8, 9, 77 }; // 匿名方法 List<int> evenList = list.FindAll(delegate (int i) { return i % 2 == 0; }); Console.WriteLine(); foreach (int item in evenList) { Console.WriteLine(item); } }}

 

3.Lambda表達式

public class MyDelegateSyntax{    public static void Show()    {        Console.WriteLine("fun with lambdas");        List<int> list = new List<int> { 20, 1, 4, 8, 9, 77 };        // Lambda表達式        // 隱式,編輯器會更具上下文表達式推斷i的類型        List<int> evenList = list.FindAll(i => i % 2 == 0);        // 顯式        // 描述:我的參數列表(一個整形i)將會被表達式(i%2)==0處理        List<int> evenList1 = list.FindAll((int i) => i % 2 == 0);        // 我的參數列表(一個整形i)將會被表達式(i%2)==0處理        Console.WriteLine();        foreach (int item in evenList)        {            Console.WriteLine(item);        }    }}

 

 4.使用多個語句處理參數(“{}”)

public class MyDelegateSyntax{    public static void Show()    {        Console.WriteLine("fun with lambdas");        List<int> list = new List<int> { 20, 1, 4, 8, 9, 77 };        // 多個處理語句 語句塊 {}        List<int> evenList = list.FindAll(i =>       {            Console.WriteLine(i);            return i % 2 == 0;        });        Console.WriteLine();        foreach (int item in evenList)        {            Console.WriteLine(item);        }    }}

 

5.含有多個(或零個)參數的Lambda表達式

public class MyDelegateSyntax{    public delegate string VerySimpleDelegate();    public delegate void MathMessage(string msg, int result);    public static void Show()    {        Console.WriteLine("fun with lambdas");        // 多個參數的Lambda        MathMessage mm = new MathMessage((msg, result) => Console.WriteLine($"msg:{msg} result:{result}"));        mm("adding has cmpleted", 1 + 5);        // 0個參數Lambda        VerySimpleDelegate d = new VerySimpleDelegate(() => "enjoy you string");        Console.WriteLine(d.Invoke());    }}

 

學無止境,望各位看官多多指教。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥芬河市| 南川市| 道孚县| 莱芜市| 洞口县| 界首市| 德钦县| 大埔县| 甘泉县| 清流县| 宜春市| 樟树市| 喀喇沁旗| 九龙县| 沂源县| 弥渡县| 湛江市| 吉水县| 马龙县| 芜湖县| 三江| 宜都市| 河津市| 九寨沟县| 砚山县| 双鸭山市| 安远县| 江孜县| 综艺| 新平| 澜沧| 北流市| 新宾| 壶关县| 尚志市| 深泽县| 湖北省| 南涧| 河西区| 邵武市| 东海县|