关于类型参数数量的过载解决失败令人困惑

时间:2016-04-08 20:21:48

标签: vb.net generics overload-resolution

在C#中,我可以写:

Enumerable.Range(1, 20).Select(i => i)

我还可以明确指定Select的两个类型参数:

Enumerable.Range(1, 20).Select<int, int>(i => i)

在VB中,第一个代码段的等效工作正常:

Enumerable.Range(1, 20).Select(Function(i) i)

但是当我尝试指定类型参数时,它会失败:

Enumerable.Range(1, 20).Select(Of Integer, Integer)(Function(i) i)

我得到的错误是:

  

BC32087重载解析失败,因为没有可访问的'[Select]'接受此数量的类型参数。

我不明白:有an overload of extension method named Select with two type parameters。我做错了什么?

1 个答案:

答案 0 :(得分:4)

尝试一下:

Enumerable.Range(1, 20).Select(Of Integer)(Function(i) i)

您之前的示例无法正常工作的原因是因为the overload that you referenced实际上是Enumerable个对象的扩展方法,因此第一个参数实际上是触发调用的对象。 / p>

您可see an example of this being used here并按预期工作。