© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
58 replies
YetAnohterOne

CA1860: Avoid using 'Enumerable.Any()' extension method

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1860

Does this rule make any sense?

The rule description says: "To determine whether a collection type has any elements, it's more efficient and clearer to use the Length, Count, or IsEmpty (if possible) properties than to call the Enumerable.Any method. Any(), which is an extension method, uses language integrated query (LINQ). It's more efficient to rely on the collection's own properties, and it also clarifies intent."

Does using Count truly clarify intent? I was always using .Any() because I thought it is far clearer -
Count != 0
Count != 0
just asks to be extracted into a method, the intent is not to check the number of elements, but whether the collection has Any elements.

I really miss an
Empty
Empty
property or method, but the next best thing is
!collection.Any()
!collection.Any()
or, even better, an extension method:
public static Empty<T>(this IEnumerable<T> enumerable) => !enumerable.Any()
public static Empty<T>(this IEnumerable<T> enumerable) => !enumerable.Any()
.

But VisualStudio yells at me now and suggests me to use
Count == 0
Count == 0
or
Count != 0
Count != 0
instead.

Also note that
Count
Count
bears the risk of accidentally messing up
!=
!=
with
==
==
or vice-versa.

The rule description mentions the
IsEmpty
IsEmpty
property, but note that the two most common cases (List and Array) have no such property.

Finally the rule mentions performance, but isn't this a typical case of premature optimization? Unless I'm in a tight loop, the 10ms saved by avoid
.Any()
.Any()
won't matter, and
.Any()
.Any()
does seem clearer, which is what matters.

Am I missing something? Are there really any outstanding reasons to avoid
Any()
Any()
? Because I feel ready to rebel and just suppress this suggestion.
CA1860: Avoid using 'Enumerable.Any()' extension method - .NET
Learn about code analyzer rule CA1860 - Avoid using 'Enumerable.Any()' extension method
CA1860: Avoid using 'Enumerable.Any()' extension method - .NET
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

Enumerable merging with Enumerable
C#CC# / help
4y ago
❔ Encapsulation with Extension Method
C#CC# / help
4y ago
Enumerable.OrderBy help
C#CC# / help
13mo ago