views:

98

answers:

1

When trying to compile the following code in LINQPad :

void Main()
{
    DriveInfo.GetDrives().Select(GetProviderName).Dump();
}

static string GetProviderName(DriveInfo drive)
{
    // some irrelevant WMI code...
}

I get the following error :

The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

If I use a lambda like d => GetProviderName(d) instead of a method group, it works fine... I'm quite surprised, because I was sure the compiler would be able to infer the type from the method group. There is no other GetProviderName method in scope, and the input and output types are clearly defined, so it should be implicitly convertible to a Func<DriveInfo, string>... shouldn't it ?

+7  A: 

This is a limitation in the compiler that was fixed in C# 4.0

SLaks
Ah, I *knew* I had seen it working before... the test above was done in C# 3.0. Do you have any reference link about that ?
Thomas Levesque
http://togaroga.com/2009/11/smarter-type-inference-with-c-4/
SLaks
just what I was looking for... thanks !
Thomas Levesque