Count or Count ()

9

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

if (minhaLista.Count > 0) ;

or

if (minhaLista.Count() > 0) ;
    
asked by anonymous 07.10.2016 / 18:43

3 answers

14

The best way is always to use Count , it is incremented whenever 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 method of extending the namespace System.Linq .

In the case of List 's, the Count() method checks whether the object is actually a ICollection and then returns the Count property (in other cases it can execute 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;
}
    
07.10.2016 / 18:49
8

In general, whenever you can use the Count property, it is best because access is direct. The expectation is that it is always O (1).

Count() is a LINQ extension method , so it can only be used on objects that implement IEnumerable that can perform some algorithm to get the count. If the object in question implements the interface ICollection what it will do is just read the Count property, the result will be the same and the response time almost identical, but slightly slightly slower by having an indirection. But both will have complexity O (1). Note that this is not guaranteed using this method, it depends on context.

The source of it can be seen in the .Net Reference Source .

    
07.10.2016 / 18:49
7

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

The Count() is an extension method that comes from the System.Linq namespace, its implementation consists of sweeping IEnumerable and counting (or attempting to fetch the Count property, if it is an implementation coming 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 thing to use is Count in this case.

    
07.10.2016 / 18:50