-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for CA1864. (#36059)
- Loading branch information
1 parent
ea53ce5
commit b9faab9
Showing
4 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
title: "CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method" | ||
description: "Learn about code analyzer rule CA1864 - Prefer the 'IDictionary.TryAdd(TKey, TValue)' method" | ||
ms.date: 06/30/2023 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1864 | ||
- PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer | ||
helpviewer_keywords: | ||
- CA1864 | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
|
||
# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method | ||
|
||
| | Value | | ||
| ----------------------------------- |----------------------------------------| | ||
| **Rule ID** | CA1864 | | ||
| **Category** | [Performance](performance-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
| **Enabled by default in .NET 7** | No | | ||
|
||
## Cause | ||
|
||
<xref:System.Collections.Generic.Dictionary%602.Add%2A?displayProperty=nameWithType> is guarded by a <xref:System.Collections.Generic.Dictionary%602.ContainsKey(%600)?displayProperty=nameWithType> call. | ||
|
||
## Rule description | ||
|
||
Both <xref:System.Collections.Generic.Dictionary%602.ContainsKey(%600)?displayProperty=nameWithType> and <xref:System.Collections.Generic.Dictionary%602.Add%2A?displayProperty=nameWithType> perform a lookup, which is redundant. <xref:System.Collections.Generic.Dictionary%602.Add%2A?displayProperty=nameWithType> also throws an exception if the key is already present in the dictionary. It's more efficient to call <xref:System.Collections.Generic.Dictionary%602.TryAdd%2A?displayProperty=nameWithType>, which returns a Boolean value that indicates if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. | ||
|
||
## How to fix violations | ||
|
||
Replace a call to <xref:System.Collections.Generic.Dictionary%602.ContainsKey(%600)?displayProperty=nameWithType> that's followed by a call to <xref:System.Collections.Generic.Dictionary%602.Add%2A?displayProperty=nameWithType> with a single call to <xref:System.Collections.Generic.Dictionary%602.TryAdd%2A?displayProperty=nameWithType>. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1864: | ||
|
||
```csharp | ||
void Run(IDictionary<int, string> dictionary) | ||
{ | ||
if(!dictionary.ContainsKey(2)) { | ||
dictionary.Add(2, "Hello World"); | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Sub Run(dictionary As IDictionary(Of Integer, String)) | ||
If Not dictionary.ContainsKey(2) Then | ||
dictionary.Add(2, "Hello World") | ||
End If | ||
End Sub | ||
``` | ||
|
||
The following code snippet fixes the violation: | ||
|
||
```csharp | ||
void Run(IDictionary<int, string> dictionary) | ||
{ | ||
dictionary.TryAdd(2, "Hello World"); | ||
} | ||
``` | ||
|
||
```vb | ||
Sub Run(dictionary As IDictionary(Of Integer, String)) | ||
dictionary.TryAdd(2, "Hello World") | ||
End Sub | ||
``` | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress this warning if performance isn't a concern and if you handle the exception that might be thrown by <xref:System.Collections.Generic.Dictionary%602.Add%2A?displayProperty=nameWithType>. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1864 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1864 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1864.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters