请问,这个Fouc委托

rdtvgdstgc3 2011-10-21 12:49:30
Func(Of T1, T2, TResult) 委托:http://msdn.microsoft.com/zh-cn/library/bb534647.aspx
MSDN的例子:
static void Main(string[] args)
{
string sentence = "the quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
string reversed = words.Aggregate((x, y) =>y + " " + x);
Console.WriteLine(reversed);
Console.ReadKey();
}

例子中的两个参数,x、y应该怎么解释啊,x可以看成是集合的每一个元素,那y呢?并没有说明 y += x 啊,也没有说明y是什么东西,怎么就能行呢?
...全文
132 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxmark 2011-10-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 rdtvgdstgc3 的回复:]
引用 3 楼 hbu_pig 的回复:

words.Aggregate((x, y) =>y + " " + x);
意思是说words中的元素之间的关系就是多了个空格连接了起来

意思就是,x、y都代笔集合的元素?那先后顺序呢?把x、y的顺序换一下,结果又不一样,这是作何解释呢?
[/Quote]

楼主应该把问题分开考虑,分成:
(x,y)=>y+" "+x

words.Aggregate();

其中(x,y)=>y+" "+x是一个lambda表达式,此处可以转化为:
public string Method(string x,string y)
{
return y+" "+x;
}
也就是把两个字符串合并为一个字符串(第一个参数在前,第二个参数在后,并且中间加空格)的函数,这个函数谁用都可以,这里的x,y只是有象征意义.

words.Aggregate()这是string[]类型提供的一个方法,这个方法的完成需要一个具体的操作,这里提供的操作就是上面lambda提供的函数。
这个方法的说明参考8楼。
bdmh 2011-10-21
  • 打赏
  • 举报
回复
这是lambda表达式,x,y就是委托参数
欢乐的小猪 2011-10-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 rdtvgdstgc3 的回复:]
引用 3 楼 hbu_pig 的回复:

words.Aggregate((x, y) =>y + " " + x);
意思是说words中的元素之间的关系就是多了个空格连接了起来

意思就是,x、y都代笔集合的元素?那先后顺序呢?把x、y的顺序换一下,结果又不一样,这是作何解释呢?
[/Quote]


结果是不一样。结果为倒序并由空格连接起来的字符串。((x, y) =>y + " " + x);表示聚集函数元素间的逻辑关系。你可以理解为第一组x,y为元素1和元素2,之后是2,3……
风骑士之怒 2011-10-21
  • 打赏
  • 举报
回复
Aggregate方法的关键代码如下:

TSource current = enumerator.Current; //获得第一个元素
while (enumerator.MoveNext()) //移动到下一个元素开始遍历
{
current = func(current, enumerator.Current);
}

第一次遍历的时候

将一个元素和第二元素的值当作回调函数的参数,将函数的操作结果返回并赋值给current

第二次遍历的时候
用返回的结果和下一个元素继续调用函数。。。。


用你的例子来分析
string sentence = "the quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
string reversed = words.Aggregate((x, y) => y + " " + x);

第一个调用
current = func("the", "quick"); //current得到的值为 "quick the"

第二次调用
current = func("quick the", "brown"); //current得到的值为 "brown quick the"

第三次调用
current = func("brown quick the", "fox"); //current得到的值为 "fox brown quick the"

。。。

以此类推

最后return current;返回这个结果,就是数组元素反过来了。


完整代码:

public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (!enumerator.MoveNext())
{
throw Error.NoElements();
}
TSource current = enumerator.Current;
while (enumerator.MoveNext())
{
current = func(current, enumerator.Current);
}
return current;
}
}
rdtvgdstgc3 2011-10-21
  • 打赏
  • 举报
回复
等待答案
rdtvgdstgc3 2011-10-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hbu_pig 的回复:]

words.Aggregate((x, y) =>y + " " + x);
意思是说words中的元素之间的关系就是多了个空格连接了起来
[/Quote]
意思就是,x、y都代笔集合的元素?那先后顺序呢?把x、y的顺序换一下,结果又不一样,这是作何解释呢?
kkbac 2011-10-21
  • 打赏
  • 举报
回复


学习
hztltgg 2011-10-21
  • 打赏
  • 举报
回复
还有这个好方法,上次我想给数据库里的一列数据变成字符串连接起来,居然不知道这个方法
1
2
4
3
====得到
1+2+4+3
看来直接
words.Aggregate((x, y) =>x + "+" + y);
就可以,我之前还每个记录又重新查询一次数据库。
欢乐的小猪 2011-10-21
  • 打赏
  • 举报
回复
words.Aggregate((x, y) =>y + " " + x);
意思是说words中的元素之间的关系就是多了个空格连接了起来
MadEric 2011-10-21
  • 打赏
  • 举报
回复
和Array.Sort一样的道理的。
x 和 y 都是委托(匿名函数)的参数,这里用的是lambda表达式。参数的类型根据推断确定。

过程是迭代的,x:"the", y:"quick" => y+ " " + x: "quick the"
x: "quick the", y: "brown" => y+ " " + x: "brown quick the"
...

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧