需要有关Enumerable.Aggregate函数的更多细节

par*_*agy 5 c# aggregate-functions

你能帮我理解吗,

       words.Aggregate((workingSentence, next) => + next + " " + workingSentence);   
Run Code Online (Sandbox Code Playgroud)

从下面的代码片段?如果有人解释我在C#1.1中实现这一点,那就太好了.

(来自MS的片段) -

        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);
        // This code produces the following output:
        //
        // dog lazy the over jumps fox brown quick the
Run Code Online (Sandbox Code Playgroud)

Luk*_*keH 6

Aggregate你的榜样的一部分转换为东西大致是这样的:

string workingSentence = null;
bool firstElement = true;
foreach (string next in words)
{
    if (firstElement)
    {
        workingSentence = next;
        firstElement = false;
    }
    else
    {
        workingSentence = next + " " + workingSentence;
    }
}
string reversed = workingSentence;
Run Code Online (Sandbox Code Playgroud)

workingSentence变量是一个累加器,其通过向现有的累加器值和该序列的当前元素的函数的循环的每次迭代更新; 这是由示例中的lambda和foreach我示例中的循环体执行的.