在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LambdaSample { class Program { static void Main(string[] args) { int[] myIntArray = { 1,2, 3, 4, 5 }; //累加器计算数组总和 int result = myIntArray.Aggregate((paramA, paramB) => paramA + paramB); int result1 = myIntArray.Aggregate<int>((paramA, paramB) => paramA + paramB); int result2 = myIntArray.Aggregate<int, int>(0, (paramA, paramB) => paramA + paramB); //递归计算数组总和 int result3 = count(5); Console.WriteLine("The Result is: " + result); Console.WriteLine("The Result1 is: " + result1); Console.WriteLine("The Result2 is: " + result2); Console.WriteLine("The Result3 is: " + result3); string[] myStrArray = { "Admin", "Change", "ByPage" }; //字符串数组拼接 string strResult = myStrArray.Aggregate((paramA, paramB) => paramA + " " + paramB); //字符串数组字符串总长度计算 int result4 = myStrArray.Aggregate<string, int>(0, (a, b) => a + b.Length); //字符串数组拼接2,增加字符串"Some curries: " string strResult1 = myStrArray.Aggregate<string, string>("Some curries: ", (a, b) => a + " " + b); //字符串数组字符串总长度计算2 int result5 = myStrArray.Aggregate<string, string, int>("Some curries: ", (a, b) => a + " " + b, a => a.Length); Console.WriteLine("The StrResult is: " + strResult); Console.WriteLine("The Result4 is: " + result4); Console.WriteLine(strResult1); Console.WriteLine("The Result5 is: " + result5); Console.ReadKey(); } static int count(int n) { if (n == 1) { return 1; } return n + count(n - 1); } } }
该例子源代码摘自C#入门经典第5版第14章(387页)
|
请发表评论