.Net: .Any vs .Count

I was reviewing some code and resharper threw up a hint to use

if(myList.Any()){ //conditional code }

instead of

if(myList.Count > 0) { //conditional code }

However, if a line of code is written like the following, it does not throw the same hint

if(myList.Count == 0) { //conditional code }

Of course, upon further review I realized that the developer was only interested to check and continue if the list was empty.

.Any() returns a boolean value which is a semantically better option than the .Count counterparts.

I was curious to know the performance impact and found an interesting response on stackoverflow.com. My understanding from the answer marked correctly is that it is a better option to use when working with IEnumerable types.

Given that in our organization we work a lot with generic lists, .Any() seems to be a good option when all that we want to know is whether the list is empty or not.


One thought on “.Net: .Any vs .Count

  1. Pingback: Kartik sehgal | Charlestonslip

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s