有3個Enumerable.Aggregate的過載版本。我無法找到該功能的任何過載版本以匹配official example中使用的版本。C#聚合函數定義說明
public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
)
上述定義是從該官方例完全不同:
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
下面是聚合函數的3個過載版本:
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);
他們中沒有匹配在所使用的函數上面的例子。官方文件是否錯誤?或者我錯過了什麼?請幫我填補3個版本的函數定義和這個官方示例之間的差距。
如何理解函數定義?
實際上,你提到的第一個重載**與在「官方示例」中使用的相同。 – Evk
查看[擴展方法](https://msdn.microsoft.com/en-us/library/bb383977.aspx)。 –