I haven't noticed this behaviour yet, maybe because i prefer query syntax in VB.NET and split the query and the execution-methods into different statements.
If i try to compile following simple query:
Dim wordList As List(Of String) = New List(Of String)
Dim longWords As Int32 = wordList.Count(Function(word) word.Length > 100)
The compiler doesn't like this because he expects no arguments for List.Count
:
"Public Readonly Property Count As Integer" has no parameters and its return type cannot be indexed.
If i declare it as IEnumerable(Of String)
it works as expected:
Dim wordSeq As IEnumerable(Of String) = New List(Of String)
Dim longWords As Int32 = wordSeq.Count(Function(word) word.Length > 100)
Why is that so? What prevents the compiler from using the Enumerable
extension method Count
instead of the ICollection.Count
property. Note that i've added Imports System.Linq
and both Option Strict
and Option Infer
are On
. I'm using.NET 4.0 (Visual Studio 2010).
I'm confused because in C# this works without a problem:
List<String> wordList = new List<String>();
int longWordCount = wordList.Count(word => word.Length > 100);