-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ca1841 #23887
Ca1841 #23887
Changes from all commits
8b06cb6
2044ce5
1a366f6
3cecaf4
800c908
50a9ca2
b3050e2
596e541
bd367df
c4d7f22
57810b7
3549b31
798b292
f5f3ed9
72d3825
06e97de
ea7974c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,99 @@ | ||||||
--- | ||||||
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 | ||||||
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 <xref:System.Collections.Generic.IDictionary%602> that could be replaced with a call to a `ContainsKey` or `ContainsValue` method on the dictionary itself. | ||||||
|
||||||
## Rule description | ||||||
|
||||||
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 <xref:System.Collections.Generic.IEnumerable%601> if the keys or values collection uses explicit interface implementation to hide methods on <xref:System.Collections.Generic.ICollection%601>. 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 <xref:System.Collections.Generic.IEnumerable%601> 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<string, int>(); | ||||||
|
||||||
// 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops sorry Youssef. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, it happened that I commented at nearly the same time of you merging 😄 |
||||||
' 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.