C#入门经典Lambda

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceLambdaSample7{8classPRogram9{10staticvoidMain(string[] args)11{12int[] myIntArray = {1,2,3,4,5};13//累加器计算数组总和14intresult = myIntArray.Aggregate((paramA, paramB) => paramA +paramB);15intresult1 = myIntArray.Aggregate<int>((paramA, paramB) => paramA +paramB);16intresult2 = myIntArray.Aggregate<int,int>(0, (paramA, paramB) => paramA +paramB);17//递归计算数组总和18intresult3 = count(5);19Console.WriteLine("The Result is:"+result);20Console.WriteLine("The Result1 is:"+result1);21Console.WriteLine("The Result2 is:"+result2);22Console.WriteLine("The Result3 is:"+result3);2324string[] myStrArray = {"Admin","Change","ByPage"};25//字符串数组拼接 何问起26stringstrResult = myStrArray.Aggregate((paramA, paramB) => paramA +""+paramB);27//字符串数组字符串总长度计算28intresult4 = myStrArray.Aggregate<string,int>(0, (a, b) => a +b.Length);29//字符串数组拼接2,增加字符串"Some curries: "30stringstrResult1 = myStrArray.Aggregate<string,string>("Some curries:", (a, b) => a +""+b);31//字符串数组字符串总长度计算2 hovertree.com32intresult5 = myStrArray.Aggregate<string,string,int>("Some curries:", (a, b) => a +""+ b, a =>a.Length);33Console.WriteLine("The StrResult is:"+strResult);34Console.WriteLine("The Result4 is:"+result4);35Console.WriteLine(strResult1);36Console.WriteLine("The Result5 is:"+result5);37Console.ReadKey();38}3940staticintcount(intn)41{42if(n ==1)43{44return1;45}46returnn + 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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航