当前位置: 首页>>代码示例>>C#>>正文


C# Enumerable.Aggregate方法代码示例

本文整理汇总了C#中System.Linq.Enumerable.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# Enumerable.Aggregate方法的具体用法?C# Enumerable.Aggregate怎么用?C# Enumerable.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了Enumerable.Aggregate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1:

string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };

// Determine whether any string in the array is longer than "banana".
string longestName =
    fruits.Aggregate("banana",
                    (longest, next) =>
                        next.Length > longest.Length ? next : longest,
    // Return the final result as an upper case string.
                    fruit => fruit.ToUpper());

Console.WriteLine(
    "The fruit with the longest name is {0}.",
    longestName);
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:13,代码来源:Enumerable.Aggregate

输出:

The fruit with the longest name is PASSIONFRUIT.

示例2:

int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };

// Count the even numbers in the array, using a seed value of 0.
int numEven = ints.Aggregate(0, (total, next) =>
                                    next % 2 == 0 ? total + 1 : total);

Console.WriteLine("The number of even integers is: {0}", numEven);
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:7,代码来源:Enumerable.Aggregate

输出:

The number of even integers is: 6

示例3:

string sentence = "the quick brown fox jumps over the lazy dog";

// Split the string into individual words.
string[] words = sentence.Split(' ');

// Prepend each word to the beginning of the 
// new sentence to reverse the word order.
string reversed = words.Aggregate((workingSentence, next) =>
                                      next + " " + workingSentence);

Console.WriteLine(reversed);
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:11,代码来源:Enumerable.Aggregate

输出:

dog lazy the over jumps fox brown quick the

示例4: Main

//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;

public class MainClass {
    public static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        var query = numbers.Aggregate((a, b) => a * b);
    }
}
开发者ID:C#程序员,项目名称:System.Linq,代码行数:13,代码来源:Enumerable.Aggregate


注:本文中的System.Linq.Enumerable.Aggregate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。