Click here to Skip to main content
Click here to Skip to main content

Tagged as

Count() and Count property

, 16 Oct 2012 CPOL
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() 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.  

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)

Share

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 - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
Suggestionit all depends on the goals PinmemberInTRUEdeR17-Oct-12 0:10 
GeneralRe: it all depends on the goals PinmemberJohn Brett18-Oct-12 0:19 
QuestionIQueryable? Pinmemberspringy7616-Oct-12 23:11 

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

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

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