In this post I am investigating the difference between Count property and Count() extension method of the .NET enumerable.

Below code snippet sets the scene of what we trying to answer.

As in the previous post will start by decompiling the System.Core 4.0.0.0 dll.

The Linq Count() extension method behaves differently based on the source implementation. If the source extends ICollection/ICollection<T> interface, then it relies on the underneath implementation Count property of the source. For instance, List<T>, IReadOnlyList<T>IDictionary<TKey, TValue>Queue, Stack, or HashSet<T> they extend ICollection, and the Count property is backed by a private field int size as shown below. So using Count property whenever is available is the most efficient.

 

One thought on “Insights on Linq Count() vs Count Property”

Leave a Reply

Your email address will not be published. Required fields are marked *