Given a list, which method is preferred to determine the number of elements inside?
var myList = new List<string>();
myList.Count
myList.Count()
Given a list, which method is preferred to determine the number of elements inside?
var myList = new List<string>();
myList.Count
myList.Count()
Count()
is an extension method introduced by LINQ while the Count
property is part of the List itself (derived from ICollection
). Internally though, LINQ checks if your IEnumerable
implements ICollection
and if it does it uses the Count
property. So at the end of the day, there's no difference which one you use for a List
.
To prove my point further, here's the code from Reflector for Enumerable.Count()
public static int Count<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
ICollection<TSource> is2 = source as ICollection<TSource>;
if (is2 != null)
{
return is2.Count;
}
int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
num++;
}
}
return num;
}
"So at the end of the day, there's no difference ..."
Apart from the fact that LINQ needed the day to decide that it uses the builtin function ;) –
Granado Count()
in case your type changes from a List
to some other IEnumerable
that doesn't have a Count
property. –
Slily Always prefer Count
and Length
properties on a type over the extension method Count()
. The former is an O(1) for every type which contains them. The Count()
extension method has some type check optimizations that can cause it to run also in O(1) time but will degrade to O(N) if the underlying collection is not one of the few types it knows about.
Any()
extension method. –
Eskimoaleut Count > 0
over Any()
. –
Longueur myList.Count is a method on the list object, it just returns the value of a field so is very fast. As it is a small method it is very likely to be inlined by the compiler (or runtime), they may then allow other optimization to be done by the compiler.
myList.Count() is calling an extension method (introduced by LINQ) that loops over all the items in an IEnumerable, so should be a lot slower.
However (In the Microsoft implementation) the Count extension method has a “special case” for Lists that allows it to use the list’s Count property, this means the Count() method is only a little slower than the Count property.
It is unlikely you will be able to tell the difference in speed in most applications.
So if you know you are dealing with a List use the Count property, otherwise if you have a "unknown" IEnumerabl, use the Count() method and let it optimise for you.
If you by any chance wants to change the type of your collection you are better served with the Count()
extension. This way you don't have to refactor your code (to use Length
for instance).
Count()
gives you 1 less than thing to worry about changing if you decide to use a different type of collection (in that .Count()
will work if it's an Array, a List, etc.) –
Definitive © 2022 - 2024 — McMap. All rights reserved.
int
?long
? Does it matter? Certainly not for this code, or for most code … – Castroprauxel