C# Enumerable Aggregate(IEnumerable, Func)
Description
Enumerable Aggregate (IEnumerable, Func)
Applies
an accumulator function over a sequence.
Syntax
Enumerable.Aggregate(IEnumerable, Func)
has the following syntax.
public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
)
Parameters
Enumerable.Aggregate(IEnumerable, Func)
has the following parameters.
TSource
- The type of the elements of source.source
- An IEnumerableto aggregate over. func
- An accumulator function to be invoked on each element.
Returns
Enumerable.Aggregate(IEnumerable, Func)
method returns TSource
Example
The following code example demonstrates how to reverse the order of words in a string by using Aggregate.
//from w ww . java2 s . c o m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
string sentence = "this is a test";
string[] words = sentence.Split(' ');
string reversed = words.Aggregate((workingSentence, next) =>
next + " " + workingSentence);
Console.WriteLine(reversed);
}
}
The code above generates the following result.