Count or Count()

Asked

Viewed 2,766 times

11

Having a list, what better way to know the number of items on this list and what the difference?

if (minhaLista.Count > 0) ;

or

if (minhaLista.Count() > 0) ;

3 answers

17


The best way is always to use Count, it is incremented every time an item is added to the list and decremented every time an item is removed.

The difference between the two is that Count is a property of List and Count() is a namespace extension method System.Linq.

In the case of List's, the method Count() checks if the object is actually a ICollection and then returns the property Count (in other cases it can run some algorithm to get the count). Using the property directly you end up avoiding this check.

Here you can find the source of the method (note the third line)

public static int Count<TSource>(this IEnumerable<TSource> source) 
{
    if (source == null) throw Error.ArgumentNull("source");
    ICollection<TSource> collectionoft = source as ICollection<TSource>;
    if (collectionoft != null) return collectionoft.Count;
    ICollection collection = source as ICollection;
    if (collection != null) return collection.Count;
    int count = 0;
    using (IEnumerator<TSource> e = source.GetEnumerator()) {
        checked {
            while (e.MoveNext()) count++;
        }
    }
    return count;
}

9

Generally whenever you can use the property Count is better because access is direct. The expectation is that it will always be O(1).

Count() is a LINQ extension method, so you can only use it on objects that implement IEnumerable that you can run some algorithm to get the count. If the object in question implements the interface ICollection what he’ll do is just read the property Count, the result will be the same and the response time almost identical, but much slower because it has an indirect. But both will have complexity O(1). Note that this is not guaranteed using this method, it depends on the context.

The source of it can be seen in the . NET Framework Reference Source. See also . NET Core (.NET 5 forward) (have to search the archives relatives since there is more organized.

  • I will use the source link in my reply. I hope you don’t mind =)

  • Of course not, he public.

8

The Count is a property manipulated by your list, which is incremented as you apply a Add in the list, that is, you have a direct access to the value when using it.

The Count() is an extension method that comes from namespace System.Linq, its implementation consists in sweeping the IEnumerable and count (or try to fetch the property Count, if it is an implementation from ICollection), that is, in the end they end up using the same property in this scenario. Here is the implementation of it:

  public static int Count<TSource>(this IEnumerable<TSource> source) {
        if (source == null) throw Error.ArgumentNull("source");
        ICollection<TSource> collectionoft = source as ICollection<TSource>;
        if (collectionoft != null) return collectionoft.Count;
        ICollection collection = source as ICollection;
        if (collection != null) return collection.Count;
        int count = 0;
        using (IEnumerator<TSource> e = source.GetEnumerator()) {
            checked {
                while (e.MoveNext()) count++;
            }
        }
        return count;
    }

The best to use is the Count in this case.

  • Count() does not count in lists, the code itself shows this

  • I was just correcting it now...

  • Show! + 1 now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.