C#入門經典Lambda

來源:互聯網
上載者:User

標籤:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace LambdaSample 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             int[] myIntArray = { 1,2, 3, 4, 5 };13             //累加器計算數組總和 14             int result = myIntArray.Aggregate((paramA, paramB) => paramA + paramB);15             int result1 = myIntArray.Aggregate<int>((paramA, paramB) => paramA + paramB);16             int result2 = myIntArray.Aggregate<int, int>(0, (paramA, paramB) => paramA + paramB);17             //遞迴計算數組總和18             int result3 = count(5);19             Console.WriteLine("The Result is: " + result);20             Console.WriteLine("The Result1 is: " + result1);21             Console.WriteLine("The Result2 is: " + result2);22             Console.WriteLine("The Result3 is: " + result3);23 24             string[] myStrArray = { "Admin", "Change", "ByPage" };25             //字串數組拼接 何問起26             string strResult = myStrArray.Aggregate((paramA, paramB) => paramA + " " + paramB);27             //字串數組字串總長度計算28             int result4 = myStrArray.Aggregate<string, int>(0, (a, b) => a + b.Length);29             //字串數組拼接2,增加字串"Some curries: "30             string strResult1 = myStrArray.Aggregate<string, string>("Some curries: ", (a, b) => a + " " + b);31             //字串數組字串總長度計算2  hovertree.com32             int result5 = myStrArray.Aggregate<string, string, int>("Some curries: ", (a, b) => a + " " + b, a => a.Length);33             Console.WriteLine("The StrResult is: " + strResult);34             Console.WriteLine("The Result4 is: " + result4);35             Console.WriteLine(strResult1);36             Console.WriteLine("The Result5 is: " + result5);37             Console.ReadKey();38         }39 40         static int count(int n)41         {42             if (n == 1)43             {44                 return 1;45             }46             return n + count(n - 1);47         }48     }49 }

轉自:http://hovertree.com/h/bjaf/jf9yeju1.htm

參考:C# Lambda 執行個體代碼 深入淺出Lambda運算式 C#泛型詳細介紹

開發技術文章收集: http://www.cnblogs.com/sosoft/p/kaifajishu.html

C#入門經典Lambda

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.