Skip to content

Commit b6e3a52

Browse files
authored
Add documentation for CA1860. (#34157)
1 parent bf23991 commit b6e3a52

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: "CA1860: Avoid using 'Enumerable.Any()' extension method"
3+
description: "Learn about code analyzer rule CA1860 - Avoid using 'Enumerable.Any()' extension method"
4+
ms.date: 03/01/2023
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA1860
8+
- PreferLengthCountIsEmptyOverAnyAnalyzer
9+
helpviewer_keywords:
10+
- CA1860
11+
dev_langs:
12+
- CSharp
13+
- VB
14+
---
15+
16+
# CA1860: Avoid using 'Enumerable.Any()' extension method
17+
18+
| | Value |
19+
| ----------------------------------- |----------------------------------------|
20+
| **Rule ID** | CA1860 |
21+
| **Category** | [Performance](performance-warnings.md) |
22+
| **Fix is breaking or non-breaking** | Non-breaking |
23+
24+
## Cause
25+
26+
<xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> is called on a type that has a `Length`, `Count`, or `IsEmpty` property.
27+
28+
## Rule description
29+
30+
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+
32+
`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.
33+
34+
## How to fix violations
35+
36+
Replace a call to <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> with a call to the collection's `Length`, `Count`, or `IsEmpty` property.
37+
38+
## Example
39+
40+
The following code snippet shows a violation of CA1860:
41+
42+
```csharp
43+
bool HasElements(string[] strings)
44+
{
45+
return strings.Any();
46+
}
47+
```
48+
49+
```vb
50+
Function HasElements(strings As String()) As Boolean
51+
Return strings.Any()
52+
End Function
53+
```
54+
55+
The following code snippet fixes the violation:
56+
57+
```csharp
58+
bool HasElements(string[] strings)
59+
{
60+
return strings.Length > 0;
61+
}
62+
```
63+
64+
```vb
65+
Function HasElements(strings As String()) As Boolean
66+
Return strings.Length > 0
67+
End Function
68+
```
69+
70+
## When to suppress warnings
71+
72+
It's safe to suppress this warning if performance isn't a concern.
73+
74+
## Suppress a warning
75+
76+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
77+
78+
```csharp
79+
#pragma warning disable CA1860
80+
// The code that's violating the rule is on this line.
81+
#pragma warning restore CA1860
82+
```
83+
84+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
85+
86+
```ini
87+
[*.{cs,vb}]
88+
dotnet_diagnostic.CA1860.severity = none
89+
```
90+
91+
To disable this entire category of rules, set the severity for the category to `none` in the [configuration file](../configuration-files.md).
92+
93+
```ini
94+
[*.{cs,vb}]
95+
dotnet_analyzer_diagnostic.category-Performance.severity = none
96+
```
97+
98+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The following table lists code quality analysis rules.
154154
> | [CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method](ca1854.md) | Prefer 'TryGetValue' over a Dictionary indexer access guarded by a 'ContainsKey' check. 'ContainsKey' and the indexer both look up the key, so using 'TryGetValue' avoids the extra lookup. |
155155
> | [CA1855: Use Span\<T>.Clear() instead of Span\<T>.Fill()](ca1855.md) | It's more efficient to call <xref:System.Span%601.Clear?displayProperty=nameWithType> than to call <xref:System.Span%601.Fill(%600)?displayProperty=nameWithType> to fill the elements of the span with a default value. |
156156
> | [CA1858: Use StartsWith instead of IndexOf](ca1858.md) | It's more efficient to call <xref:System.String.StartsWith%2A?displayProperty=nameWithType> than to call <xref:System.String.IndexOf%2A?displayProperty=nameWithType> to check whether a string starts with a given prefix. |
157+
> | [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md) | 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. |
157158
> | [CA2000: Dispose objects before losing scope](ca2000.md) | Because an exceptional event might occur that will prevent the finalizer of an object from running, the object should be explicitly disposed before all references to it are out of scope. |
158159
> | [CA2002: Do not lock on objects with weak identity](ca2002.md) |An object is said to have a weak identity when it can be directly accessed across application domain boundaries. A thread that tries to acquire a lock on an object that has a weak identity can be blocked by a second thread in a different application domain that has a lock on the same object. |
159160
> | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a <xref:System.Threading.Tasks.Task> directly. When an asynchronous method awaits a <xref:System.Threading.Tasks.Task> directly, continuation occurs in the same thread that created the task. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. Consider calling <xref:System.Threading.Tasks.Task.ConfigureAwait(System.Boolean)?displayProperty=nameWithType> to signal your intention for continuation. |

docs/fundamentals/code-analysis/quality-rules/performance-warnings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ Performance rules support high-performance libraries and applications.
6767
| [CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method](ca1854.md) | Prefer 'TryGetValue' over a Dictionary indexer access guarded by a 'ContainsKey' check. 'ContainsKey' and the indexer both look up the key, so using 'TryGetValue' avoids the extra lookup. |
6868
| [CA1855: Use Span\<T>.Clear() instead of Span\<T>.Fill()](ca1855.md) | It's more efficient to call <xref:System.Span%601.Clear?displayProperty=nameWithType> than to call <xref:System.Span%601.Fill(%600)?displayProperty=nameWithType> to fill the elements of the span with a default value. |
6969
| [CA1858: Use StartsWith instead of IndexOf](ca1858.md) | It's more efficient to call <xref:System.String.StartsWith%2A?displayProperty=nameWithType> than to call <xref:System.String.IndexOf%2A?displayProperty=nameWithType> to check whether a string starts with a given prefix. |
70+
| [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md) | 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. |

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ items:
783783
href: ../../fundamentals/code-analysis/quality-rules/ca1855.md
784784
- name: CA1858
785785
href: ../../fundamentals/code-analysis/quality-rules/ca1858.md
786+
- name: CA1860
787+
href: ../../fundamentals/code-analysis/quality-rules/ca1860.md
786788
- name: SingleFile rules
787789
items:
788790
- name: Overview

0 commit comments

Comments
 (0)