2017-04-03 101 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); 

两个第一字符串输入,最后一个是返回值的类型,在这个例子是两个字符串的聚合。