LINQ扩展SelectMany在3.5 vs 4.0中吗? [英] LINQ extension SelectMany in 3.5 vs 4.0?

查看:48
本文介绍了LINQ扩展SelectMany在3.5 vs 4.0中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在这里看到达林斯的建议时..

When I saw Darins suggestion here ..

IEnumerable<Process> processes = 
    new[] { "process1", "process2" } 
    .SelectMany(Process.GetProcessesByName);

( process.getprocessesbyname())

..我有点感兴趣,我在VS2008中使用.NET 3.5进行了尝试-除非将其更改为..,否则它不会编译.

.. I was a bit intrigued and I tried it in VS2008 with .NET 3.5 - and it did not compiling unless I changed it to ..

IEnumerable<Process> res = 
  new string[] { "notepad", "firefox", "outlook" }
    .SelectMany(s => Process.GetProcessesByName(s));

在我怀疑是问题所在之前,已经阅读了一些达林斯的答案,后来当我将.NET 4.0应用于VS2010时(如预期的那样),最初的建议效果很好.

Having read some Darins answers before I suspected that it was me that were the problem, and when I later got my hands on a VS2010 with.NET 4.0 - as expected - the original suggestion worked beautifully.

我的问题是:从3.5到4.0发生了什么,这才使这种(新语法)成为可能?是已经扩展的扩展方法(hmm)还是lambda语法的新规则?还是

My question is: What have happened from 3.5 to 4.0 that makes this (new syntax) possible? Is it the extensionmethods that have been extended(hmm) or new rules for lambda syntax or?

推荐答案

在新版本的C#(C#4.0与C#3.0 ...而不是.NET版本)中,委托选择似乎更加智能. )这个想法在VS2008中可用,但在解决多个重载时解决使用哪个版本的方法时遇到了问题.该方法是在编译时选择的,因此我必须相信,这与更新的编译器有关,而不是与.NET版本有关.您可能会发现,可以将新的重载功能与在VS2010中为.NET 2.0编译的解决方案一起使用.

It seems that the delegate selection is much more intelligent in the new version of C# (C# 4.0 vs. C# 3.0... not the version of .NET.) This idea was available in VS2008, but it had problems resolving which version of the method to use when there were multiple overloads. The method is selected at compilation, so I have to believe that this has more to do with the updated compiler than with the version of .NET. You will probably find that you can use the new overload ability with solutions compiled for .NET 2.0 in VS2010.

例如,这在VS2008中有效

For example, this works in VS2008

var ret = new[] { "Hello", "World", "!!!" }.Aggregate(Path.Combine);
// this is the value of ret => Hello\World\!!!

这篇关于LINQ扩展SelectMany在3.5 vs 4.0中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆