I have not noticed this behavior yet, perhaps because I prefer the query syntax in VB.NET and separate the request and execution methods into different operators.
If I try to compile the 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 does not like this because it does not expect arguments for List.Count :
The "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 this so? What prevents the compiler from using the Enumerable Count extension method instead of the ICollection.Count property. Notice that I added Imports System.Linq , and Option Strict and Option Infer On . I am using .NET 4.0 (Visual Studio 2010).
I am confused because in C # this works without problems:
List<String> wordList = new List<String>(); int longWordCount = wordList.Count(word => word.Length > 100);
Tim schmelter
source share