尝试使用 Visual Basic 中的Enumerable.Count()扩展方法时,以下代码会导致编译时错误:
Imports System.Linq
Module Module1
    Sub Main()
        Dim l As New List(Of Foo) From {New Foo("a"), New Foo("b"), New Foo("a")}
        Dim i As Integer = l.Count(Function(foo) foo.Bar = "a")
        Console.WriteLine(i)
        Console.ReadLine()
    End Sub
    Class Foo
        Sub New(ByVal bar As String)
            Me.Bar = bar
        End Sub
        Public Property Bar As String
    End Class
End Module
产生的错误是:
'Public ReadOnly Property Count As Integer' 没有参数,它的返回类型不能被索引。
我的目标是 .NET 4.0,所以应该支持扩展方法。还值得注意的是,C# 中的等效代码正确地推断出扩展方法......
为什么编译器无法推断 Enumerable.Count 的使用,给定我作为参数传递的谓词,以及如何使用扩展方法而不是 List 的 Count 属性?