Click here to Skip to main content
13,045,657 members (65,681 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 16 Oct 2012

Count() and Count property

, 16 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Count() and Count property

In this tip post I am going to discuss about the Count property and Count() method that used to return count of number of element in collection.

Count property

Each collection object which is inherited from ICollection<T> Interface has count property which returns number of element in collection.

Count() Function

But the things change when you make use of System.Linq namespace in you code. when you make use of this namespace you get Count()<t> method which also returns you number of element in you collection. But the point to not here is Count() is extestion method of IEnumerable<T> class. Check the following images

Without using Linq namespace

With Using Linq namespace

IEnumerable<T> Source after query

After Converting Source to ICollection<T> type

Code on which I tested
List<string> lst = new List<string>() { "abc", "def" };
int a = lst.Count;
var b = lst.Where(x => x == "abc").Count();
List<string> ls = lst.Where(x => x == "abc").ToList<string>();
a= ls.Count;

If you are using Count() method on source which implements ICollection<T> interface than extension method make use of the Count property of it and returns no of element. If the source not implemented from the ICollection<T> than it do perform the operation on the element of source and return count of it.

Point to Note

  •  As per MSDN : Retrieving the value of Count property is an O(1) operation.
  • Count() function perform the operation and return value, so its slower than count property.  


Although its stated that Count() function make use of count property if the source implemented from ICollection<T> than use cont property, its better to use count property directly if source implemented ICollection<T> otherwise go for Count() get number of element in source. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...

Comments and Discussions

Suggestionit all depends on the goals Pin
InTRUEdeR16-Oct-12 23:10
memberInTRUEdeR16-Oct-12 23:10 
GeneralRe: it all depends on the goals Pin
John Brett17-Oct-12 23:19
memberJohn Brett17-Oct-12 23:19 
QuestionIQueryable? Pin
springy7616-Oct-12 22:11
memberspringy7616-Oct-12 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Oct 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid