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?

finoutlook
  • 2,523
  • 5
  • 29
  • 43

3 Answers3

12

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

Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81
  • 1
    it's not property of a class it's property of an Interface `ICollection`, look here http://msdn.microsoft.com/pl-pl/library/92t2ye13(v=vs.110).aspx – Kamil Budziewski Dec 11 '13 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 Dec 11 '13 at 11:56
  • The property `Count` in class `List` belongs to `List` class. Doesn't matter if this class implements `ICollection` 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` and `ICollection` as example but this concept works for any case of class that implements interface.) – Jonny Piazzi Dec 11 '13 at 12:27
  • I guess the property is still around for backward compatibility? – finoutlook Dec 12 '13 at 12:32
  • @finoutlook I thought your question is very good, so I created a new topic question and already answered. See in http://stackoverflow.com/q/20551124/1233788 – Jonny Piazzi Dec 12 '13 at 18:18
  • @JonnyPiazzi: The one situation where this would be slightly muddy would be if it were implemented explicitly, such that you couldn't refer to it directly if you had an expression of the class type rather than the interface type. Other than that, I agree completely. – Jon Skeet Dec 12 '13 at 18:26
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.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
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;
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459