From 80ae970861f6c4e024e053b2efa750b3ef85abe8 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Fri, 14 Jul 2023 08:16:09 -0400 Subject: [PATCH 01/11] reference CA1860 from CA127 update docs for CA1827 to refer to the possible ambiguity with CA1860 --- docs/fundamentals/code-analysis/quality-rules/ca1827.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index 74ca08b9ed171..1026864eaea3a 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1827.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1827.md @@ -23,11 +23,11 @@ ms.author: mavasani ## Cause -The or method was used where the method would be more efficient. +The () or () **method** was used where the () 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 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. ## How to fix violations @@ -61,6 +61,9 @@ class C } ``` +> [!NOTE] +> This rule is similar to [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md). However and that rule applies suggests using the `Count` **property**, while this rule applies to the , this Linq `Count()` extension **method**. + > [!TIP] > A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose **Do not use Count() or LongCount() when Any() can be used** from the list of options that's presented. > @@ -94,6 +97,7 @@ 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) +- [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md) ## See also From c33bae5f29aaeeb83c94579404465f5fb781df55 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Fri, 14 Jul 2023 08:27:29 -0400 Subject: [PATCH 02/11] Update ca1860.md updated ca 1860 to call out the similarity to the ca1827 analyzer --- .../fundamentals/code-analysis/quality-rules/ca1860.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1860.md b/docs/fundamentals/code-analysis/quality-rules/ca1860.md index 14830c2582dfd..515d7afa16398 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1860.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1860.md @@ -24,7 +24,7 @@ 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 @@ -68,6 +68,9 @@ Function HasElements(strings As String()) As Boolean End Function ``` +> [!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 suggest the use of the `Count` **property**. + ## When to suppress warnings It's safe to suppress this warning if performance isn't a concern. @@ -90,3 +93,8 @@ 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) From 2aba2bac36ff71d422db4b74306fb10470bd7638 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Fri, 14 Jul 2023 08:28:46 -0400 Subject: [PATCH 03/11] Update ca1827.md fix typos --- docs/fundamentals/code-analysis/quality-rules/ca1827.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index 1026864eaea3a..e5eccb0c266b0 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1827.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1827.md @@ -62,7 +62,7 @@ class C ``` > [!NOTE] -> This rule is similar to [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md). However and that rule applies suggests using the `Count` **property**, while this rule applies to the , this Linq `Count()` extension **method**. +> 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**. > [!TIP] > A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose **Do not use Count() or LongCount() when Any() can be used** from the list of options that's presented. From 8d5c002684a8f06cf74e029b977a1c5efeaf7fcc Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Fri, 14 Jul 2023 10:52:18 -0400 Subject: [PATCH 04/11] fix missing blanks around list fix missing blanks around list --- docs/fundamentals/code-analysis/quality-rules/ca1860.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1860.md b/docs/fundamentals/code-analysis/quality-rules/ca1860.md index 515d7afa16398..8452af347b3fc 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1860.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1860.md @@ -95,6 +95,7 @@ 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) From 80b335139b399a45642f28de8ca3d8b0bfc20317 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Sun, 16 Jul 2023 12:44:36 -0400 Subject: [PATCH 05/11] Update docs/fundamentals/code-analysis/quality-rules/ca1827.md Update title of error Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1827.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index e5eccb0c266b0..21b87ec116770 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1827.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1827.md @@ -23,7 +23,7 @@ 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()]( Date: Sun, 16 Jul 2023 12:44:55 -0400 Subject: [PATCH 06/11] Update docs/fundamentals/code-analysis/quality-rules/ca1827.md Accept suggestions Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1827.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index 21b87ec116770..e884be399eadd 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1827.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1827.md @@ -27,7 +27,7 @@ The [Count()](xref:System.Linq.Enumerable.Count%2A) or [LongCount()](xref:System ## 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()]( Date: Sun, 16 Jul 2023 12:45:33 -0400 Subject: [PATCH 07/11] Update docs/fundamentals/code-analysis/quality-rules/ca1860.md Accept formatting suggestion Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1860.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1860.md b/docs/fundamentals/code-analysis/quality-rules/ca1860.md index 8452af347b3fc..9c29f6ace553e 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1860.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1860.md @@ -69,7 +69,7 @@ End Function ``` > [!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 suggest the use of the `Count` **property**. +> 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*. ## When to suppress warnings From 776311564d9f6f328535129d3f59def84e5a4c6e Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Sun, 16 Jul 2023 12:46:00 -0400 Subject: [PATCH 08/11] Update docs/fundamentals/code-analysis/quality-rules/ca1827.md Accept formatting and wording suggesting Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1827.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index e884be399eadd..1533be55b7d62 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1827.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1827.md @@ -62,7 +62,7 @@ class C ``` > [!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**. +> 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*. > [!TIP] > A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose **Do not use Count() or LongCount() when Any() can be used** from the list of options that's presented. From 2eb3a37359c8e3fc8bee947071acac535a4fffc3 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Sun, 16 Jul 2023 12:46:28 -0400 Subject: [PATCH 09/11] Update docs/fundamentals/code-analysis/quality-rules/ca1860.md Accept formatting suggestion Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1860.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1860.md b/docs/fundamentals/code-analysis/quality-rules/ca1860.md index 9c29f6ace553e..e7b5633e69a3b 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1860.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1860.md @@ -24,7 +24,7 @@ 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 From ffd564e624692388c501ebf7e51d2aaf6ef01b68 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:26:03 -0700 Subject: [PATCH 10/11] Move note block and fix xrefs --- .../code-analysis/quality-rules/ca1827.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1827.md b/docs/fundamentals/code-analysis/quality-rules/ca1827.md index 1533be55b7d62..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 [Count()](xref:System.Linq.Enumerable.Count%2A) or [LongCount()](xref:System.Linq.Enumerable.LongCount%2A) *method* was used where the [Any()]( [!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 @@ -61,9 +64,6 @@ class C } ``` -> [!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*. - > [!TIP] > A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose **Do not use Count() or LongCount() when Any() can be used** from the list of options that's presented. > @@ -96,7 +96,7 @@ 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 From 31c7e116cbda699e8f4115dd9361f13ed0412e64 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:27:54 -0700 Subject: [PATCH 11/11] Move note block up --- .../code-analysis/quality-rules/ca1860.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1860.md b/docs/fundamentals/code-analysis/quality-rules/ca1860.md index e7b5633e69a3b..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 @@ -28,13 +28,16 @@ dev_langs: ## 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 @@ -68,9 +71,6 @@ Function HasElements(strings As String()) As Boolean End Function ``` -> [!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*. - ## When to suppress warnings It's safe to suppress this warning if performance isn't a concern.