2017-04-03 104 views
1

有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個版本的函數定義和這個官方示例之間的差距。

如何理解函數定義?

+0

實際上,你提到的第一個重載**與在「官方示例」中使用的相同。 – Evk

+0

查看[擴展方法](https://msdn.microsoft.com/en-us/library/bb383977.aspx)。 –

回答

3
public static TSource Aggregate<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, TSource, TSource> func 
) 

這實際上是在官方的例子中使用的:

string reversed = words.Aggregate((workingSentence, next) => 
             next + " " + workingSentence); 

有些作品可能會混淆你:

  1. source參數前面的this關鍵字將其標識爲擴展方法,這意味着上述代碼爲語法糖:

    string reversed = Enumerable.Aggregate(words, (workingSentence, next) => 
                next + " " + workingSentence); 
    
  2. TSource是一個通用的參數,在這種情況下取​​代有string因爲編譯器能夠推斷這種類型的事實,wordsIEnumerable<string>

  3. Func<TSource, TSource, TSource>是一個代表具有兩個參數(前兩個TSource s)並返回TSource的函數的一般代表。這與Action<TSource, TSource>相反,這將需要兩個參數並且不返回值。這些類型中的任何一種都可以用表達式(param1, param2) => expression的lambda表達式表示。

+0

Func func函數需要3個參數嗎?或者前2個是2個參數類型,第3個類型是返回類型? –

+0

@ NicolasS.Xu:後者。查看我的更新。 – StriplingWarrior

2

您給出的示例使用了您發佈的第一個重載。這可能是因爲這是一種將你拋棄的擴展方法。

使用擴展方法,this參數成爲對其被調用的實例的引用。編譯代碼時,會將其轉換爲適當的靜態方法調用。基本上,當你這樣寫:

words.Aggregate((workingSentence, next) => 
            next + " " + workingSentence); 

它編譯成這樣:

Enumerable.Aggregate<string>(words, (workingSentence, next) => 
            next + " " + workingSentence); 
1

this修飾符指示過載是Extension Method

因此在這種情況下,方法定義中的this IEnumerable<TSource> source對應於示例中的words

1

官方文件是完全正確的。在官方示例下面簽名用於:

public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func); 

它是一個擴展方法然後串[]字作爲字符串的數組IEnumerable的源相匹配。而下面的代碼是匹配Func鍵FUNC:

(workingSentence, next) => next + " " + workingSentence 

換句話說,你可以這樣寫:在FUNC

Func<string, string, string> func = (workingSentence, next) => next + " " + workingSentence; 
words.Aggregate(func); 

兩個第一字符串輸入,最後一個是返回值的類型,在這個例子是兩個字符串的聚合。