You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The <xref:System.Linq.Enumerable.Count%2A> or <xref:System.Linq.Enumerable.LongCount%2A>method was used where the <xref:System.Linq.Enumerable.Any%2A> method would be more efficient.
26
+
The [Count()](xref:System.Linq.Enumerable.Count%2A) or [LongCount()](xref:System.Linq.Enumerable.LongCount%2A)*method* was used where the [Any()](xref:System.Linq.Enumerable.Any%2A) method would be more efficient.
27
27
28
28
## Rule description
29
29
30
-
This rule flags the <xref:System.Linq.Enumerable.Count%2A> and <xref:System.Linq.Enumerable.LongCount%2A> LINQ method calls used to check if the collection has at least one element. These method calls require enumerating the entire collection to compute the count. The same check is faster with the <xref:System.Linq.Enumerable.Any%2A> method as it avoids enumerating the collection.
30
+
This rule flags [Count()](xref:System.Linq.Enumerable.Count%2A) and [LongCount()](xref:System.Linq.Enumerable.LongCount%2A) LINQ method calls that are used to check if the collection has at least one element. These methods enumerate the entire collection to compute the count. The same check is faster with the [Any()](xref:System.Linq.Enumerable.Any%2A) method as it avoids enumerating the collection.
31
+
32
+
> [!NOTE]
33
+
> This rule is similar to [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md). However that rule suggests using the `Count`*property*, while this rule applies to the Linq `Count()`*extension method*.
31
34
32
35
## How to fix violations
33
36
@@ -93,7 +96,8 @@ For more information, see [How to suppress code analysis warnings](../suppress-w
93
96
94
97
-[CA1826: Use property instead of Linq Enumerable method](ca1826.md)
95
98
-[CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used](ca1828.md)
96
-
-[CA1829: Use Length/Count property instead of Enumerable.Count method](ca1829.md)
99
+
-[CA1829: Use Length/Count property instead of Enumerable.Count() method](ca1829.md)
100
+
-[CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md)
Copy file name to clipboardExpand all lines: docs/fundamentals/code-analysis/quality-rules/ca1860.md
+13-4Lines changed: 13 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "CA1860: Avoid using 'Enumerable.Any()' extension method"
3
3
description: "Learn about code analyzer rule CA1860 - Avoid using 'Enumerable.Any()' extension method"
4
-
ms.date: 03/01/2023
4
+
ms.date: 07/17/2023
5
5
ms.topic: reference
6
6
f1_keywords:
7
7
- CA1860
@@ -24,17 +24,20 @@ dev_langs:
24
24
25
25
## Cause
26
26
27
-
<xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> is called on a type that has a `Length`, `Count`, or `IsEmpty` property.
27
+
<xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> is called on a type that has a `Length`, `Count`, or `IsEmpty`*property*.
28
28
29
29
## Rule description
30
30
31
-
It's more efficient and clearer to use `Length`, `Count`, or `IsEmpty` (if possible) than to call <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType>to determine whether a collection type has any elements.
31
+
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 <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType>method.
32
32
33
33
`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.
34
34
35
+
> [!NOTE]
36
+
> This rule is similar to [CA1827: Do not use Count()/LongCount() when Any() can be used](ca1827.md). However, that rule applies to the Linq `Count()`*method*, while this rule suggests using the `Count`*property*.
37
+
35
38
## How to fix violations
36
39
37
-
Replace a call to <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> with a call to the collection's `Length`, `Count`, or `IsEmpty` property.
40
+
Replace a call to [Any()](xref:System.Linq.Enumerable.Any%2A) with a call to the collection's `Length`, `Count`, or `IsEmpty` property.
0 commit comments