ReSharper 2016.1 Help

Code Inspection: Use method Any()

In a code snippet like this:

if (myEnumerable.Count() > 0)

ReSharper suggests replacing the Count() > 0 part with the Any() extension method for two reasons. First, Any() is generally quicker as it does not iterate through the whole collection. Second, Any() improves the readability of your code, indicating that the developer just wants to check if there are any items in the collection and isn't concerned with the exact number of items.

Please note that if your type derives from ICollection<T> then you can use the Count property which is definitely faster than both Any() and Count(). Please make sure you don't confuse the Count property with the Count() extension method.

See Also

Last modified: 19 August 2016