开发者

Why generic type inference doesn't work in that case?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜