|
| 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). |
0 commit comments