你的位置:首页 > 软件开发 > ASP.net > C# Lambda表达式使用累加器例子

C# Lambda表达式使用累加器例子

发布时间:2016-01-20 12:00:07
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LambdaSample{ class Program { static void M ...
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LambdaSample{  class Program  {    static void Main(get='_blank'>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# Lambda表达式使用累加器例子

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录