Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / C#
Tip/Trick

Count() and Count property

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
16 Oct 2012CPOL1 min read 18.2K   4   3
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

<t>
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.  

Conclusion  


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. 

License

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


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin 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 - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
Suggestionit all depends on the goals Pin
intrueder16-Oct-12 23:10
intrueder16-Oct-12 23:10 
GeneralRe: it all depends on the goals Pin
John Brett17-Oct-12 23:19
John Brett17-Oct-12 23:19 
QuestionIQueryable? Pin
springy7616-Oct-12 22:11
springy7616-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.