zoukankan      html  css  js  c++  java
  • C# Lambda表达式使用累加器例子

    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页)

  • 相关阅读:
    PyCharm中的Console自动换行
    Jenkins 配置用户权限错误导致无法登录解决方案
    Jenkins进阶-用户权限管理(10)
    python3+Flask 链接MySQL 时,提示“No module named MYSQLdb”
    Mongo导出mongoexport和导入mongoimport介绍
    maven常用命令
    Git 的origin和master分析
    How to handle your webdriver exceptions
    Java中equals和==的区别
    Maven中-DskipTests和-Dmaven.test.skip=true的区别
  • 原文地址:https://www.cnblogs.com/qiu2013/p/5144487.html
Copyright © 2011-2022 走看看