From 8b06cb63ea5bc1e92a31ff63e72a41928bd770c6 Mon Sep 17 00:00:00 2001 From: NewellClark Date: Thu, 22 Apr 2021 15:24:50 -0400 Subject: [PATCH 01/17] Add ca1841.md --- .../code-analysis/quality-rules/ca1841.md | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/ca1841.md diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md new file mode 100644 index 0000000000000..576922bce2692 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -0,0 +1,100 @@ +--- +title: "CA1841: Prefer Dictionary contains methods (code analysis)" +description: "Learn about code analysis rule CA1841: Prefer Dictionary contains methods" +ms.date: 04/21/2021 +ms.topic: reference +f1_keywords: + - "PreferDictionaryContainsMethods" + - "CA1841" +helpviewer_keywords: + - "PreferDictionaryContainsMethods" + - "CA1841" +author: NewellClark +ms.author: NewellClark +dev_langs: + - CSharp + - VB +--- +# CA1841: Prefer Dictionary contains methods + +| | Value | +|-|-| +| **Rule ID** |CA1841 +| **Category** |[Performance](performance-warnings.md)| +| **Fix is breaking or non-breaking** |Non-breaking| + +## Cause + +This rule locates calls to a `Contains` method on the `Keys` or `Values` collection of an that could be replaced with a call to an accessible `ContainsKey` or `ContainsValue` method on the dictionary itself. + +## Rule description + +Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than simply calling `ContainsKey` or `ContainsValue` on the dictionary itself: + +- Many dictionary implementations lazily instantiate the key and value collections, which means that accessing the `Keys` or `Values` collection may result in extra allocations. +- You may end up calling an extension method on if the keys or values collection uses explicit interface implementation to hide methods on . This can lead to reduced performance, especially when accessing the key collection, as most dictionary implementations are able to provide a fast O(1) containment check for keys, while the `Contains` extension method on usually does a slow O(n) containment check. + +## How to fix violations + +To fix violations, replace calls to `dictionary.Keys.Contains` or `dictionary.Values.Contains` with calls to `dictionary.ContainsKey` or `dictionary.ContainsValue`, respectively. + +The following code snippet shows examples of violations, and how to fix them. + +```csharp +using System.Collections.Generic; +// Importing this namespace brings extension methods for IEnumerable`1 into scope. +using System.Linq; + +class Example +{ + void Method() + { + var dictionary = new Dictionary(); + + // Violation + dictionary.Keys.Contains("hello world"); + + // Fixed + dictionary.ContainsKey("hello world"); + + // Violation + dictionary.Values.Contains(17); + + // Fixed + dictionary.ContainsValue(17); + } +} +``` + +```vb +Imports System.Collection.Generic +' Importing this namespace brings extension methods for IEnumerable`1 into scope. +' Note that in Visual Basic, this namespace is often imported automatically throughout the project. +Imports System.Linq + +Class Example + Private Sub Method() + Dim dictionary = New Dictionary(Of String, Of Integer) + + ' Violation + dictionary.Keys.Contains("hello world") + + ' Fixed + dictionary.ContainsKey("hello world") + + ' Violation + dictionary.Values.Contains(17) + + ' Fixed + dictionary.ContainsValue(17) + End Sub +End Class +``` + +## When to suppress warnings + +It is safe to suppress warnings from this rule if the code in question is not performance-critical. + +## See also + +- [Performance rules](performance-warnings.md) \ No newline at end of file From 2044ce564a76e2fb4be75a28d824cda904e357cc Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Thu, 22 Apr 2021 15:37:13 -0400 Subject: [PATCH 02/17] Add CA1841 to toc.yml --- docs/fundamentals/toc.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 7c8909d61cdec..59a88f9ff4a96 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -776,6 +776,8 @@ items: href: code-analysis/quality-rules/ca1837.md - name: CA1838 href: code-analysis/quality-rules/ca1838.md + - name: CA1841 + href: code-analysis/quality-rules/ca1841.md - name: Publish rules items: - name: Overview From 1a366f68f013c8352f41456dadfd8f96c9290e5b Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Thu, 22 Apr 2021 15:43:05 -0400 Subject: [PATCH 03/17] Add CA1841 to index.md --- docs/fundamentals/code-analysis/quality-rules/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 90f5111ca1916..62de61577f8f4 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -129,6 +129,7 @@ The following table lists code quality analysis rules. > |[CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, or to determine whether the object contains or not any items. | > | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. | > | [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. | +> | [CA1841: Prefer Dictionary contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than simply calling `ContainsKey` or `ContainsValue` on the dictionary itself. | > | [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. | > |[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. | > | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a directly. When an asynchronous method awaits a 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 to signal your intention for continuation. | From 3cecaf40a069b717123ceca7991c30d75833692b Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Thu, 22 Apr 2021 15:44:16 -0400 Subject: [PATCH 04/17] Update index.md --- docs/fundamentals/code-analysis/quality-rules/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 62de61577f8f4..4becbfc4a24e2 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -129,7 +129,7 @@ The following table lists code quality analysis rules. > |[CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, or to determine whether the object contains or not any items. | > | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. | > | [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. | -> | [CA1841: Prefer Dictionary contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than simply calling `ContainsKey` or `ContainsValue` on the dictionary itself. | +> | [CA1841: Prefer Dictionary contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself. | > | [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. | > |[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. | > | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a directly. When an asynchronous method awaits a 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 to signal your intention for continuation. | From 800c908fada6c0fff6a021aabe9b2b255404715b Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Thu, 22 Apr 2021 15:47:18 -0400 Subject: [PATCH 05/17] Add CA1841 to performance-warnings.md --- .../code-analysis/quality-rules/performance-warnings.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md index 44feb8ad3cdce..182a177b74214 100644 --- a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md @@ -49,3 +49,4 @@ Performance rules support high-performance libraries and applications. | [CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, or to determine whether the object contains or not any items. | | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. | | [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of `StringBuilder` always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. | +| [CA1841: Prefer Dictionary contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself. | From 50a9ca2f02182787a30e79ff8280bce17ff0753a Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Thu, 22 Apr 2021 15:53:56 -0400 Subject: [PATCH 06/17] Fix whitespace issues --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 576922bce2692..428688e7c5ea9 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -36,9 +36,9 @@ Calling `Contains` on the `Keys` or `Values` collection may often be more expens ## How to fix violations -To fix violations, replace calls to `dictionary.Keys.Contains` or `dictionary.Values.Contains` with calls to `dictionary.ContainsKey` or `dictionary.ContainsValue`, respectively. +To fix violations, replace calls to `dictionary.Keys.Contains` or `dictionary.Values.Contains` with calls to `dictionary.ContainsKey` or `dictionary.ContainsValue`, respectively. -The following code snippet shows examples of violations, and how to fix them. +The following code snippet shows examples of violations, and how to fix them. ```csharp using System.Collections.Generic; @@ -97,4 +97,4 @@ It is safe to suppress warnings from this rule if the code in question is not pe ## See also -- [Performance rules](performance-warnings.md) \ No newline at end of file +- [Performance rules](performance-warnings.md) From b3050e234fa153cbe631b6241894eccaf3d19179 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Thu, 22 Apr 2021 22:13:19 -0400 Subject: [PATCH 07/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Youssef Victor --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 428688e7c5ea9..eec5d8e7281c0 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -32,7 +32,7 @@ This rule locates calls to a `Contains` method on the `Keys` or `Values` collect Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than simply calling `ContainsKey` or `ContainsValue` on the dictionary itself: - Many dictionary implementations lazily instantiate the key and value collections, which means that accessing the `Keys` or `Values` collection may result in extra allocations. -- You may end up calling an extension method on if the keys or values collection uses explicit interface implementation to hide methods on . This can lead to reduced performance, especially when accessing the key collection, as most dictionary implementations are able to provide a fast O(1) containment check for keys, while the `Contains` extension method on usually does a slow O(n) containment check. +- You may end up calling an extension method on if the keys or values collection uses explicit interface implementation to hide methods on . This can lead to reduced performance, especially when accessing the key collection, as most dictionary implementations are able to provide a fast O(1) containment check for keys, while the `Contains` extension method on usually does a slow O(n) containment check. ## How to fix violations From 596e54126065622c1754949dbfa3d1b419b614bf Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:33:51 -0400 Subject: [PATCH 08/17] Update docs/fundamentals/code-analysis/quality-rules/index.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 4becbfc4a24e2..496d29981912a 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -129,7 +129,7 @@ The following table lists code quality analysis rules. > |[CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, or to determine whether the object contains or not any items. | > | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. | > | [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. | -> | [CA1841: Prefer Dictionary contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself. | +> | [CA1841: Prefer Dictionary Contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself. | > | [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. | > |[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. | > | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a directly. When an asynchronous method awaits a 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 to signal your intention for continuation. | From bd367dfbf4b530aa1c0803f6680ffdedfabf3a6b Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:34:03 -0400 Subject: [PATCH 09/17] Update docs/fundamentals/code-analysis/quality-rules/performance-warnings.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../code-analysis/quality-rules/performance-warnings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md index 182a177b74214..a0512d73ab367 100644 --- a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md @@ -49,4 +49,4 @@ Performance rules support high-performance libraries and applications. | [CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, or to determine whether the object contains or not any items. | | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. | | [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of `StringBuilder` always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. | -| [CA1841: Prefer Dictionary contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself. | +| [CA1841: Prefer Dictionary Contains methods](ca1841.md) | Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself. | From c4d7f22c0f52c8e12c8480d5030aba9fa1ff2b63 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:34:09 -0400 Subject: [PATCH 10/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index eec5d8e7281c0..59a3d152511dc 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -1,5 +1,5 @@ --- -title: "CA1841: Prefer Dictionary contains methods (code analysis)" +title: "CA1841: Prefer Dictionary Contains methods (code analysis)" description: "Learn about code analysis rule CA1841: Prefer Dictionary contains methods" ms.date: 04/21/2021 ms.topic: reference From 57810b786544c73c57f5bf394bc1a9d108f569b8 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:34:17 -0400 Subject: [PATCH 11/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 59a3d152511dc..43d8fa39229f1 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -1,6 +1,6 @@ --- title: "CA1841: Prefer Dictionary Contains methods (code analysis)" -description: "Learn about code analysis rule CA1841: Prefer Dictionary contains methods" +description: "Learn about code analysis rule CA1841: Prefer Dictionary Contains methods" ms.date: 04/21/2021 ms.topic: reference f1_keywords: From 3549b31a67e5f8d1cce27575a5c7d209f65ac809 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:35:03 -0400 Subject: [PATCH 12/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 43d8fa39229f1..37d8f461c47af 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -10,7 +10,6 @@ helpviewer_keywords: - "PreferDictionaryContainsMethods" - "CA1841" author: NewellClark -ms.author: NewellClark dev_langs: - CSharp - VB From 798b292eefdeac28b7b630ce35a455732ed16f06 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:35:13 -0400 Subject: [PATCH 13/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 37d8f461c47af..3fd030a2df33a 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -14,7 +14,7 @@ dev_langs: - CSharp - VB --- -# CA1841: Prefer Dictionary contains methods +# CA1841: Prefer Dictionary Contains methods | | Value | |-|-| From f5f3ed9ea9f3bc46100e89e6fe9832f66ee148a2 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:35:20 -0400 Subject: [PATCH 14/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 3fd030a2df33a..414f3ef8cca1e 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -18,7 +18,7 @@ dev_langs: | | Value | |-|-| -| **Rule ID** |CA1841 +| **Rule ID** |CA1841| | **Category** |[Performance](performance-warnings.md)| | **Fix is breaking or non-breaking** |Non-breaking| From 72d3825630d70d032f2b71df3125c10a9d033bfb Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:36:16 -0400 Subject: [PATCH 15/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 414f3ef8cca1e..c875e74703ce6 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -24,7 +24,7 @@ dev_langs: ## Cause -This rule locates calls to a `Contains` method on the `Keys` or `Values` collection of an that could be replaced with a call to an accessible `ContainsKey` or `ContainsValue` method on the dictionary itself. +This rule locates calls to a `Contains` method on the `Keys` or `Values` collection of an that could be replaced with a call to a `ContainsKey` or `ContainsValue` method on the dictionary itself. ## Rule description From 06e97de159eb8b2fa469689293ccec40e3c27b58 Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:36:25 -0400 Subject: [PATCH 16/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index c875e74703ce6..69acdf6bf6173 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -28,7 +28,7 @@ This rule locates calls to a `Contains` method on the `Keys` or `Values` collect ## Rule description -Calling `Contains` on the `Keys` or `Values` collection may often be more expensive than simply calling `ContainsKey` or `ContainsValue` on the dictionary itself: +Calling `Contains` on the `Keys` or `Values` collection can often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself: - Many dictionary implementations lazily instantiate the key and value collections, which means that accessing the `Keys` or `Values` collection may result in extra allocations. - You may end up calling an extension method on if the keys or values collection uses explicit interface implementation to hide methods on . This can lead to reduced performance, especially when accessing the key collection, as most dictionary implementations are able to provide a fast O(1) containment check for keys, while the `Contains` extension method on usually does a slow O(n) containment check. From ea7974c38c7f6b3abed97e6010149f4f88aeb3cf Mon Sep 17 00:00:00 2001 From: Newell Clark Date: Fri, 23 Apr 2021 00:38:21 -0400 Subject: [PATCH 17/17] Update docs/fundamentals/code-analysis/quality-rules/ca1841.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1841.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1841.md b/docs/fundamentals/code-analysis/quality-rules/ca1841.md index 69acdf6bf6173..3caed8a4acac6 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1841.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1841.md @@ -31,7 +31,7 @@ This rule locates calls to a `Contains` method on the `Keys` or `Values` collect Calling `Contains` on the `Keys` or `Values` collection can often be more expensive than calling `ContainsKey` or `ContainsValue` on the dictionary itself: - Many dictionary implementations lazily instantiate the key and value collections, which means that accessing the `Keys` or `Values` collection may result in extra allocations. -- You may end up calling an extension method on if the keys or values collection uses explicit interface implementation to hide methods on . This can lead to reduced performance, especially when accessing the key collection, as most dictionary implementations are able to provide a fast O(1) containment check for keys, while the `Contains` extension method on usually does a slow O(n) containment check. +- You may end up calling an extension method on if the keys or values collection uses explicit interface implementation to hide methods on . This can lead to reduced performance, especially when accessing the key collection. Most dictionary implementations are able to provide a fast O(1) containment check for keys, while the `Contains` extension method on usually does a slow O(n) containment check. ## How to fix violations