tags:

views:

94

answers:

1

Hi

When I saw Darins suggestion here ..

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

( http://stackoverflow.com/questions/3059667/process-getprocessesbyname/3059733#3059733 )

.. 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));

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.

My question is : What have happend from 3.5 to 4.0 that makes this (new syntax) possible? Is it the extentionmethods that have been extended(hmm) or new rules for lambda syntax or? I've tried to search but my google-fu was not strong enough.

Please forgive if the question is a bit naive and note that I've taged it as beginner :)

+5  A: 

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.

For example, this works in VS2008

var ret = new[] { "Hello", "World", "!!!" }.Aggregate(Path.Combine);
// this is the value of ret => Hello\World\!!!
Matthew Whited
Ok, thanks a lot for the effort :)
Moberg