Count property vs Count() method?
C#LinqListCollectionsCountC# Problem Overview
Working with a collection I have the two ways of getting the count of objects; Count
(the property) and Count()
(the method). Does anyone know what the key differences are?
I might be wrong, but I always use the Count
property in any conditional statements because I'm assuming the Count()
method performs some sort of query against the collection, where as Count
must have already been assigned prior to me 'getting.' But that's a guess - I don't know if performance will be affected if I'm wrong.
EDIT: Out of curiosity then, will Count()
throw an exception if the collection is null? Because I'm pretty sure the Count
property simply returns 0.
C# Solutions
Solution 1 - C#
Decompiling the source for the Count()
extension method reveals that it tests whether the object is an ICollection
(generic or otherwise) and if so simply returns the underlying Count
property:
So, if your code accesses Count
instead of calling Count()
, you can bypass the type checking - a theoretical performance benefit but I doubt it would be a noticeable one!
// System.Linq.Enumerable
public static int Count<TSource>(this IEnumerable<TSource> source)
{
checked
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
ICollection<TSource> collection = source as ICollection<TSource>;
if (collection != null)
{
return collection.Count;
}
ICollection collection2 = source as ICollection;
if (collection2 != null)
{
return collection2.Count;
}
int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
num++;
}
}
return num;
}
}
Solution 2 - C#
Performance is only one reason to choose one or the other. Choosing .Count()
means that your code will be more generic. I've had occasions where I refactored some code that no longer produced a collection, but instead something more generic like an IEnumerable, but other code broke as a result because it depended on .Count
and I had to change it to .Count()
. If I made a point to use .Count()
everywhere, the code would likely be more reusable and maintainable. Usually opting to utilize the more generic interfaces if you can get away with it is your best bet. By more generic, I mean the simpler interface that is implemented by more types, and thus netting you greater compatibility between code.
I'm not saying .Count()
is better, I'm just saying there's other considerations that deal more with the reusability of the code you are writing.
Solution 3 - C#
The .Count()
method might be smart enough, or know about the type in question, and if so, it might use the underlying .Count
property.
Then again, it might not.
I would say it is safe to assume that if the collection has a .Count
property itself, that's going to be your best bet when it comes to performance.
If the .Count()
method doesn't know about the collection, it will enumerate over it, which will be an O(n) operation.
Solution 4 - C#
Count()
method is an extension method that iterates each element of an IEnumerable<>
and returns how many elements are there. If the instance of IEnumerable
is actually a List<>
, so it's optimized to return the Count
property instead of iterating all elements.
Solution 5 - C#
The Count()
method is the LINQ method that works on any IEnumerable<>
. You would expect the Count()
method to iterate over the whole collection to find the count, but I believe the LINQ code actually has some optimizations in there to detect if a Count property exists and if so use that.
So they should both do almost identical things. The Count property is probably slightly better since there doesn't need to be a type check in there.
Solution 6 - C#
Count()
is there as an extension method from LINQ - Count
is a property on List
s, actual .NET collection objects.
As such, Count()
will almost always be slower, since it will enumerate the collection / queryable object. On a list, queue, stack etc, use Count
. Or for an array - Length
.
Solution 7 - C#
If there is a Count
or Length
property, you should always prefer that to the Count()
method, which generally iterates the entire collection to count the number of elements within. Exceptions would be when the Count()
method is against a LINQ to SQL or LINQ to Entities source, for example, in which case it would perform a count query against the datasource. Even then, if there is a Count
property, you would want to prefer that, since it likely has less work to do.
Solution 8 - C#
Short Version: If you have the choice between a Count
property and a Count()
method always choose the property.
The difference is mainly around the efficiency of the operation. All BCL collections which expose a Count
property do so in an O(1) fashion. The Count()
method though can, and often will, cost O(N). There are some checks to try and get it to O(1) for some implementations but it's by no means guaranteed.
Solution 9 - C#
The Count()
method has an optimisation for ICollection<T>
which results in the Count
property being called. In this case there is probably no significant difference in performance.
There are types other than ICollection<T>
which have more efficient alternatives to the Count()
extension method though. This code analysis performance rule fires on the following types.
CA1829: Use Length/Count property instead of Enumerable.Count method
System.Array
System.Collections.Immutable.ImmutableArray<T>
System.Collections.ICollection
System.Collections.Generic.ICollection<T>
System.Collections.Generic.IReadOnlyCollection<T>
So, we should use Count
and Length
properties if they are available and fallback to the Count()
extension method otherwise.