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 c开发者_如何学编程learly defined, so it should be implicitly convertible to a Func<DriveInfo, string>
... shouldn't it ?
This is a limitation in the compiler that was fixed in C# 4.0
精彩评论