Skip to content

Commit 2be50a8

Browse files
Clarify CA1860 and CA1827 (#36257)
1 parent 8e0e508 commit 2be50a8

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

docs/fundamentals/code-analysis/quality-rules/ca1827.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "CA1827: Do not use Count/LongCount when Any can be used (code analysis)"
33
description: "Learn about code analysis rule CA1827: Do not use Count/LongCount when Any can be used"
4-
ms.date: 04/24/2020
4+
ms.date: 07/17/2023
55
ms.topic: reference
66
f1_keywords:
77
- "DoNotUseCountWhenAnyCanBeUsedAnalyzer"
@@ -12,7 +12,7 @@ helpviewer_keywords:
1212
author: mavasani
1313
ms.author: mavasani
1414
---
15-
# CA1827: Do not use Count/LongCount when Any can be used
15+
# CA1827: Do not use Count()/LongCount() when Any() can be used
1616

1717
| | Value |
1818
| ----------------------------------- | -------------------------------------- |
@@ -23,11 +23,14 @@ ms.author: mavasani
2323

2424
## Cause
2525

26-
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.
2727

2828
## Rule description
2929

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*.
3134
3235
## How to fix violations
3336

@@ -93,7 +96,8 @@ For more information, see [How to suppress code analysis warnings](../suppress-w
9396

9497
- [CA1826: Use property instead of Linq Enumerable method](ca1826.md)
9598
- [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)
97101

98102
## See also
99103

docs/fundamentals/code-analysis/quality-rules/ca1860.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "CA1860: Avoid using 'Enumerable.Any()' extension method"
33
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
55
ms.topic: reference
66
f1_keywords:
77
- CA1860
@@ -24,17 +24,20 @@ dev_langs:
2424

2525
## Cause
2626

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*.
2828

2929
## Rule description
3030

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.
3232

3333
`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.
3434

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+
3538
## How to fix violations
3639

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.
3841

3942
## Example
4043

@@ -90,3 +93,9 @@ dotnet_diagnostic.CA1860.severity = none
9093
```
9194

9295
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
96+
97+
## Related rules
98+
99+
- [CA1827: Do not use Count/LongCount when Any can be used](ca1827.md)
100+
- [CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used](ca1828.md)
101+
- [CA1829: Use Length/Count property instead of Enumerable.Count method](ca1829.md)

0 commit comments

Comments
 (0)