How do I specify Enumerable.Count() instead of List.Count?
extension-methods, vb.net
Solution
To answer your question as to why VB can't do what C# can in this case...
VB lets you access properties with `()` after the name, and also lets you call functions with no parameters by omitting the `()`. Also indexers use rounded brackets, instead of square brackets you have in C#. These are examples of tremendous VB features designed to make programming easier, which actually results in more ambiguous, harder to understand, and bug prone code.
So, in this particular case, VB sees you are accessing Count, and assumes the brackets after it are an indexer to the Count property, rather than arguments to the Count function.
C# sees the rounded brackets, and realises that you aren't accessing the indexer, you must be calling a function, so looks for a function.
Of course, there's room for ambiguity in C# as well. For example, a property with the same name as an extension method, which returns a delegate type will be called in preference to the extension method...
public Action Count { get; set; }
Ah... happy days.
As to how to call the IEnumerable.Count() function, a cast (preferably `DirectCast()`) or executing the extension method directly `Enumerable.Count(...)`, is far far preferable to creating a whole new array to call count on...!
Problem
When attempting to use the Enumerable.Count() extension method from Visual Basic, the following code results in a compile-time error: ``` 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 ``` The error produced is: 'Public ReadOnly Property Count As Integer' has no parameters and its return type cannot be indexed. I'm targeting .NET 4.0, so extension methods should be supported. It's also worth noting that the equivalent code in C# infers the extension method correctly... Why is the compiler unable to infer the use of Enumerable.Count, given the predicate I'm passing as an argument, and how can I use the extension method instead of the List's Count property?