JetBrains Rider 2024.1 Help

Code Inspection: Use method Any()

Category

Common Practices and Code Improvements

ID

UseMethodAny.0

EditorConfig

resharper_use_method_any_0_highlighting

Default severity

Suggestion

Language

C#

Requires SWA

No

In a code snippet like this:

if (myEnumerable.Count() > 0)

JetBrains Rider suggests replacing the Count() > 0 part with the Any() extension method for two reasons. First, Any() without parameters is quicker than Count() 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.

Last modified: 10 April 2024