diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index 74ca08b9ed171..e9bb5d0a6467c 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1827.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1827.md @@ -1,7 +1,7 @@ --- title: "CA1827: Do not use Count/LongCount when Any can be used (code analysis)" description: "Learn about code analysis rule CA1827: Do not use Count/LongCount when Any can be used" -ms.date: 04/24/2020 +ms.date: 07/17/2023 ms.topic: reference f1_keywords: - "DoNotUseCountWhenAnyCanBeUsedAnalyzer" @@ -12,7 +12,7 @@ helpviewer_keywords: author: mavasani ms.author: mavasani --- -# CA1827: Do not use Count/LongCount when Any can be used +# CA1827: Do not use Count()/LongCount() when Any() can be used | | Value | | ----------------------------------- | -------------------------------------- | @@ -23,11 +23,14 @@ ms.author: mavasani ## Cause -The or method was used where the method would be more efficient. +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. ## Rule description -This rule flags the and 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 method as it avoids enumerating the collection. +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. + +> [!NOTE] +> 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*. ## How to fix violations @@ -93,7 +96,8 @@ For more information, see [How to suppress code analysis warnings](../suppress-w - [CA1826: Use property instead of Linq Enumerable method](ca1826.md) - [CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used](ca1828.md) -- [CA1829: Use Length/Count property instead of Enumerable.Count method](ca1829.md) +- [CA1829: Use Length/Count property instead of Enumerable.Count() method](ca1829.md) +- [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md) ## See also diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1860.md b/docs/fundamentals/code-analysis/quality-rules/ca1860.md index 14830c2582dfd..841795a985eed 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1860.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1860.md @@ -1,7 +1,7 @@ --- title: "CA1860: Avoid using 'Enumerable.Any()' extension method" description: "Learn about code analyzer rule CA1860 - Avoid using 'Enumerable.Any()' extension method" -ms.date: 03/01/2023 +ms.date: 07/17/2023 ms.topic: reference f1_keywords: - CA1860 @@ -24,17 +24,20 @@ dev_langs: ## Cause - is called on a type that has a `Length`, `Count`, or `IsEmpty` property. + is called on a type that has a `Length`, `Count`, or `IsEmpty` *property*. ## Rule description -It's more efficient and clearer to use `Length`, `Count`, or `IsEmpty` (if possible) than to call to determine whether a collection type has any elements. +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 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. +> [!NOTE] +> 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*. + ## How to fix violations -Replace a call to with a call to the collection's `Length`, `Count`, or `IsEmpty` property. +Replace a call to [Any()](xref:System.Linq.Enumerable.Any%2A) with a call to the collection's `Length`, `Count`, or `IsEmpty` property. ## Example @@ -90,3 +93,9 @@ dotnet_diagnostic.CA1860.severity = none ``` For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). + +## Related rules + +- [CA1827: Do not use Count/LongCount when Any can be used](ca1827.md) +- [CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used](ca1828.md) +- [CA1829: Use Length/Count property instead of Enumerable.Count method](ca1829.md)