7

If I have a list of items (e.g. List<string> items) in C#, I can use both:

items.Count()

and

items.Count

to get the total number of items. Is there a reason for them both being available? Why not just have the method .Count()?

I notice that if I filter the list (and and end up with an IEnumerable<string>):

items.Distinct().Count()

then .Count() has to be used. So why does List<string> allow .Count at all?

3 Answers 3

12

Because one is an (LINQ) extension method on System.Linq.Enumerable and the other is a property on the class.

6
  • 1
    it's not property of a class it's property of an Interface ICollection, look here msdn.microsoft.com/pl-pl/library/92t2ye13(v=vs.110).aspx Commented Dec 11, 2013 at 11:53
  • 3
    @wudzik how is it not property of a class if it's defined in the interface :) Interface is a signature not implementation. Hence it IS a property of a class it just happens to be enforced by the interface.
    – Alex
    Commented Dec 11, 2013 at 11:56
  • The property Count in class List<T> belongs to List<T> class. Doesn't matter if this class implements ICollection<T> interface, the property still belongs to the class. A interface define a contract, let me put in this words: "If you (class) want to be marked as a my (interface) implementation, you must have these requirements.", the requirements (in this case Count property) belong to the class. (I'm using List<T> and ICollection<T> as example but this concept works for any case of class that implements interface.) Commented Dec 11, 2013 at 12:27
  • I guess the property is still around for backward compatibility?
    – finoutlook
    Commented Dec 12, 2013 at 12:32
  • @finoutlook I thought your question is very good, so I created a new topic question and already answered. See in stackoverflow.com/q/20551124/1233788 Commented Dec 12, 2013 at 18:18
3

The Count property is not an extension, but a property of ICollection<T> and hence of List<T>.

The Count() function is a LINQ extension. It works by actually counting the number of items in the collection, rather than just keeping count in a field, like the Count property does.

EDIT And apparently, Count() will call Count if it is available.

3
  • 1
    The implementation of Count() will short-circuit on the Count property if the underlying type has it. Commented Dec 11, 2013 at 11:36
  • 1
    Count is from ICollection not List Commented Dec 11, 2013 at 11:38
  • @wudzik Oh right, apologies, I'll edit my comment while I still can. I was confused, standard arrays should be caught under ICollection anyway I think. Commented Dec 11, 2013 at 11:40
2

Because it is Count property of List<T> class. And thus List<T> is IEnumerable<T> you have Count() extension also available.

BTW Count() extension will simply return value of Count property if enumerable source implements ICollection interface (where this property declared):

ICollection is3 = source as ICollection;
if (is3 != null)
    return is3.Count;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.