Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does .Count work without parentheses?

Tags:

c#

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?

like image 920
finoutlook Avatar asked Dec 11 '13 11:12

finoutlook


1 Answers

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

like image 89
Jonny Piazzi Avatar answered Oct 09 '22 12:10

Jonny Piazzi