Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<T> Any or Count? [duplicate]

Tags:

c#

linq

c#-4.0

When I want to do something with a list I first check it if is not null or contains no elements (not to blow a foreach) and I usually use list.Any() but what is the best option - to use list.Count > 0 , or to use list.Any()?

like image 900
Dominating Avatar asked Apr 21 '11 08:04

Dominating


People also ask

How do you count duplicates in a list?

If you want to count duplicates for a given element then use the count() function. Use a counter() function or basics logic combination to find all duplicated elements in a list and count them in Python.

Which is better count or any?

Any() is ALWAYS faster than . Count() > 0 ).

Why use Any instead of Count?

If your collection is in the form of an IEnumerable, the Count() method will iterate through all elements, whereas Any() won't have to. So for enumerables, Any() will have a (potentially significant) performance benefit. In your example, however, Pets is an array, and so you would be better off using .

Can list have duplicates in C#?

Place all items in a set and if the count of the set is different from the count of the list then there is a duplicate. Should be more efficient than Distinct as there is no need to go through all the list. Don't call list. Count() method.


5 Answers

  • Use Count if you're using a List, since it knows its size.
  • Use Length for an Array
  • If you just have an IEnumerable I would use .Any() over .Count() as it will be faster since it stops after checking one item.

Also check out this question: Which method performs better: .Any() vs .Count() > 0?

like image 156
Ray Avatar answered Sep 30 '22 12:09

Ray


I use list.Count > 0 just because it doesn't depend on the LINQ methods and so works on C# 2.0.

I personally avoid LINQ like the plague (because of its slow speed), and there's no reason to use extension methods here at all anyway.

However, a better solution would probably be to make your own version of Any that would take in a null reference, and return true if it's a collection with elements. That would save you the null check.

like image 28
user541686 Avatar answered Sep 29 '22 12:09

user541686


.Any() is generally better to use than .Count() > 0. The reason for this is that if the items you are iterating over is not an ICollection then it will have to iterate the whole list to get the count.

But if the items is an ICollection (which a List<T> is) then it is just as fast or in some cases faster to use Count() (Any() iterates once regardless of underlying type in MS .Net but Mono tries to optimize this to Count > 0 when the underlying items is an ICollection)

A great tool is Reflector, the .Net source code and the Mono source code which allows you to see how things are implemented.

like image 25
Lasse Espeholt Avatar answered Oct 03 '22 12:10

Lasse Espeholt


If you are using the Entity Framework and have a huge table with many records Any() will be much faster. I remember one time I wanted to check to see if a table was empty and it had millions of rows. It took 20-30 seconds for Count() > 0 to complete. It was instant with Any().

like image 29
ashlar64 Avatar answered Sep 30 '22 12:09

ashlar64


Any() can be a performance enhancement because it may not have to iterate the collection to get the number of things. It just has to hit one of them. Or, for, say, LINQ-to-Entities, the generated SQL will be IF EXISTS(...) rather than SELECT COUNT ... or even SELECT * ....

like image 44
Janmejay Kumar Avatar answered Sep 29 '22 12:09

Janmejay Kumar