|
| 1 | +--- |
| 2 | +title: "CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method" |
| 3 | +description: "Learn about code analyzer rule CA1864 - Prefer the 'IDictionary.TryAdd(TKey, TValue)' method" |
| 4 | +ms.date: 06/30/2023 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | + - CA1864 |
| 8 | + - PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer |
| 9 | +helpviewer_keywords: |
| 10 | + - CA1864 |
| 11 | +dev_langs: |
| 12 | + - CSharp |
| 13 | + - VB |
| 14 | +--- |
| 15 | + |
| 16 | +# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method |
| 17 | + |
| 18 | +| | Value | |
| 19 | +| ----------------------------------- |----------------------------------------| |
| 20 | +| **Rule ID** | CA1864 | |
| 21 | +| **Category** | [Performance](performance-warnings.md) | |
| 22 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 23 | +| **Enabled by default in .NET 7** | No | |
| 24 | + |
| 25 | +## Cause |
| 26 | + |
| 27 | +<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. |
| 28 | + |
| 29 | +## Rule description |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +## How to fix violations |
| 34 | + |
| 35 | +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>. |
| 36 | + |
| 37 | +## Example |
| 38 | + |
| 39 | +The following code snippet shows a violation of CA1864: |
| 40 | + |
| 41 | +```csharp |
| 42 | +void Run(IDictionary<int, string> dictionary) |
| 43 | +{ |
| 44 | + if(!dictionary.ContainsKey(2)) { |
| 45 | + dictionary.Add(2, "Hello World"); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +```vb |
| 51 | +Sub Run(dictionary As IDictionary(Of Integer, String)) |
| 52 | + If Not dictionary.ContainsKey(2) Then |
| 53 | + dictionary.Add(2, "Hello World") |
| 54 | + End If |
| 55 | +End Sub |
| 56 | +``` |
| 57 | + |
| 58 | +The following code snippet fixes the violation: |
| 59 | + |
| 60 | +```csharp |
| 61 | +void Run(IDictionary<int, string> dictionary) |
| 62 | +{ |
| 63 | + dictionary.TryAdd(2, "Hello World"); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +```vb |
| 68 | +Sub Run(dictionary As IDictionary(Of Integer, String)) |
| 69 | + dictionary.TryAdd(2, "Hello World") |
| 70 | +End Sub |
| 71 | +``` |
| 72 | + |
| 73 | +## When to suppress warnings |
| 74 | + |
| 75 | +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>. |
| 76 | + |
| 77 | +## Suppress a warning |
| 78 | + |
| 79 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 80 | + |
| 81 | +```csharp |
| 82 | +#pragma warning disable CA1864 |
| 83 | +// The code that's violating the rule is on this line. |
| 84 | +#pragma warning restore CA1864 |
| 85 | +``` |
| 86 | + |
| 87 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 88 | + |
| 89 | +```ini |
| 90 | +[*.{cs,vb}] |
| 91 | +dotnet_diagnostic.CA1864.severity = none |
| 92 | +``` |
| 93 | + |
| 94 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
0 commit comments