HastSet<T> implements ICollection<T> that has the Count property, so calling Count() will just call HastSet<T>.Count , which I assume is an O (1) operation (this means t really needs count - it just returns the current size of the HashSet ).
Any will iterate until it finds an element matching the condition and then stops.
So, in your case, it just iterates over one element and then stops, so the difference is likely to be negligible .
If you have a filter that you want to apply (for example, x => x.IsValid ), then Any will definitely be faster, since Count(x => x.IsValid) will interact with the exentire collection, and Any will stop as soon as will find a match.
For these reasons, I usually prefer to use Any() rather than Count()==0 , as it is more direct and avoids any potential performance issues. I would switch to Count()==0 if it would provide a significant performance boost over Any() .
Note that Any(x=>true) logically matches the call to Any() . This does not change your question, but it looks cleaner without lambda.
source share