Method count count vs Count ()?

Working with the collection I have two ways to get the number of objects; Count (property) and Count () method. Does anyone know what are the key differences? Maybe I'm wrong, but I always use the Count property in any conditional statements because I assume that the Count () method does some sort of query against the collection where, as a graph, it must have been assigned before I " got". But this is an assumption - I do not know if performance will affect me if I am wrong.

EDIT: Out of curiosity, will Count () throw an exception if the collection is zero? Since I'm sure the Count property just returns 0.

+49
collections list c # linq count
Nov 01 '11 at 16:11
source share
8 answers

Decompiling the source for the Count() extension method shows that it checks to see if the ICollection (generic or otherwise), and if so it simply returns the underlying Count property:

So, if your code accesses Count instead of calling Count() , you can bypass type checking - a theoretical performance advantage, but I doubt it would be noticeable!

 // 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; } } 
+66
Nov 01 '11 at 16:18
source share

Performance is one of the reasons for choosing one or the other. Choosing .Count () means your code will be more general. I had cases where I reorganized some code that no longer created a collection, but instead became something more general, like IEnumerable, but the other code broke because it depended on .Count , and I had to change it on .Count() , If I tried to use .Count() everywhere, the code would most likely be more reusable and maintainable. Usually preferring to use more general interfaces, if you succeed, this is your best bet. More generally, I mean a simpler interface that is implemented by more types, and thus you get more compatibility between the code.

I'm not saying .Count() better, I'm just talking about other considerations that are more about reusing the code you write.

+23
03 Oct '12 at 16:23
source share

The .Count() method can be smart enough or know about the type in question, and if so, it can use the basic .Count property.

Then it may not be.

I would say that it is safe to assume that if a collection has a .Count property, this would be your best bet when it comes to performance.

If the .Count() method does not know about the collection, it will list it, which will be an O (n) operation.

+16
Nov 01 '11 at 16:16
source share

The Count () method is an extension method that iterates over each IEnumerable <> element and returns the number of elements. If the IEnumerable instance is actually List <>, so it is optimized to return the Count property instead of iterating through all the elements.

+5
Nov 01 '11 at 16:17
source share

If there is a Count or Length property, you should always use the Count () method, which usually iterates the entire collection to count the number of elements inside. (An exception would be when the Count () method refers, for example, to a Linq to SQL or Linq to Entities source, in which case it will query the counter for the data source. Even if there is a Count property, you would prefer this, since he probably has less work.)

+2
Nov 01 '11 at 16:15
source share

The Count () method is a LINQ method that works with any IEnumerable <>. You would expect the Count () method to iterate over the entire collection to find the counter, but I think there are some optimizations in the LINQ code to find out if the Count property exists and if so is used.

Therefore, they should do almost the same thing. The Count property is probably a little better since it does not require type checking.

+2
Nov 01 '11 at 16:17
source share

Short version: if you have a choice between the Count and Count() property, always select a property.

The difference is mainly related to the effectiveness of the operation. All BCL collections that set the Count property do this in O (1) fashion. The Count() method, although it can and will often cost O (N). There are some checks to try to get it in O (1) for some implementations, but this is by no means guaranteed.

+2
Nov 01 '11 at 16:20
source share

Count() exists as an extension method from LINQ - Count is a property in List s, valid .NET collection objects.

This way, Count() will almost always be slower, as it will enumerate the collection / queryable object. On a list, in a queue, on a stack, etc. Use Count . Or for an array - Length .

+1
Nov 01 '11 at 16:15
source share



All Articles