Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/fundamentals/code-analysis/quality-rules/ca1864.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method"
description: "Learn about code analyzer rule CA1864 - Prefer the 'IDictionary.TryAdd(TKey, TValue)' method"
title: "CA1864: Prefer the 'Dictionary.TryAdd(TKey, TValue)' method"
description: "Learn about code analyzer rule CA1864 - Prefer the 'Dictionary.TryAdd(TKey, TValue)' method"
ms.date: 06/30/2023
f1_keywords:
- CA1864
Expand All @@ -12,12 +12,12 @@ dev_langs:
- VB
---

# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
# CA1864: Prefer the 'Dictionary.TryAdd(TKey, TValue)' method

| Property | Value |
|-------------------------------------|------------------------------------------------------|
| **Rule ID** | CA1864 |
| **Title** | Prefer the 'IDictionary.TryAdd(TKey, TValue)' method |
| **Title** | Prefer the 'Dictionary.TryAdd(TKey, TValue)' method |
| **Category** | [Performance](performance-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 10** | As suggestion |
Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ The following table lists code quality analysis rules.
> | [CA1861: Avoid constant arrays as arguments](ca1861.md) | Constant arrays passed as arguments are not reused which implies a performance overhead. Consider extracting them to `static readonly` fields to improve performance. |
> | [CA1862: Use the `StringComparison` method overloads to perform case-insensitive string comparisons](ca1862.md) | When code calls <xref:System.String.ToLower> or <xref:System.String.ToUpper> to perform a case-insensitive string comparison, an unnecessary allocation is performed. |
> | [CA1863: Use `CompositeFormat`](ca1863.md) | To reduce the formatting cost, cache and use a <xref:System.Text.CompositeFormat> instance as the argument to `String.Format` or `StringBuilder.AppendFormat`. |
> | [CA1864: Prefer the `IDictionary.TryAdd(TKey, TValue)` method](ca1864.md) | Both <xref:System.Collections.Generic.Dictionary`2.ContainsKey(`0)?displayProperty=nameWithType> and <xref:System.Collections.Generic.Dictionary`2.Add*?displayProperty=nameWithType> perform a lookup, which is redundant. It's is more efficient to call <xref:System.Collections.Generic.Dictionary`2.TryAdd*?displayProperty=nameWithType>, which returns a `bool` indicating if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. |
> | [CA1864: Prefer the `Dictionary.TryAdd(TKey, TValue)` method](ca1864.md) | Both <xref:System.Collections.Generic.Dictionary`2.ContainsKey(`0)?displayProperty=nameWithType> and <xref:System.Collections.Generic.Dictionary`2.Add*?displayProperty=nameWithType> perform a lookup, which is redundant. It's more efficient to call <xref:System.Collections.Generic.Dictionary`2.TryAdd*?displayProperty=nameWithType>, which returns a `bool` indicating if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. |
> | [CA1865-CA1867: Use char overload](ca1865-ca1867.md) | The char overload is a better performing overload for a string with a single char. |
> | [CA1868: Unnecessary call to `Contains` for sets](ca1868.md) | Both <xref:System.Collections.Generic.ISet`1.Add(`0)?displayProperty=nameWithType> and <xref:System.Collections.Generic.ICollection`1.Remove(`0)?displayProperty=nameWithType> perform a lookup, which makes it redundant to call <xref:System.Collections.Generic.ICollection`1.Contains(`0)?displayProperty=nameWithType> beforehand. It's more efficient to call <xref:System.Collections.Generic.ISet`1.Add(`0)> or <xref:System.Collections.Generic.ICollection`1.Remove(`0)> directly, which returns a Boolean value indicating whether the item was added or removed. |
> | [CA1869: Cache and reuse `JsonSerializerOptions` instances](ca1869.md) | Using a local instance of <xref:System.Text.Json.JsonSerializerOptions> for serialization or deserialization can substantially degrade the performance of your application if your code executes multiple times, since System.Text.Json internally caches serialization-related metadata into the provided instance. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Performance rules support high-performance libraries and applications.
| [CA1861: Avoid constant arrays as arguments](ca1861.md) | Constant arrays passed as arguments are not reused which implies a performance overhead. Consider extracting them to 'static readonly' fields to improve performance. |
| [CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons](ca1862.md) | When code calls <xref:System.String.ToLower> or <xref:System.String.ToUpper> to perform a case-insensitive string comparison, an unnecessary allocation is performed. |
| [CA1863: Use 'CompositeFormat'](ca1863.md) | To reduce the formatting cost, cache and use a <xref:System.Text.CompositeFormat> instance as the argument to `String.Format` or `StringBuilder.AppendFormat`. |
| [CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method](ca1864.md) | Both <xref:System.Collections.Generic.Dictionary`2.ContainsKey(`0)?displayProperty=nameWithType> and <xref:System.Collections.Generic.Dictionary`2.Add*?displayProperty=nameWithType> perform a lookup, which is redundant. It's is more efficient to call <xref:System.Collections.Generic.Dictionary`2.TryAdd*?displayProperty=nameWithType>, which returns a `bool` indicating if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. |
| [CA1864: Prefer the 'Dictionary.TryAdd(TKey, TValue)' method](ca1864.md) | Both <xref:System.Collections.Generic.Dictionary`2.ContainsKey(`0)?displayProperty=nameWithType> and <xref:System.Collections.Generic.Dictionary`2.Add*?displayProperty=nameWithType> perform a lookup, which is redundant. It's more efficient to call <xref:System.Collections.Generic.Dictionary`2.TryAdd*?displayProperty=nameWithType>, which returns a `bool` indicating if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. |
| [CA1865-CA1867: Use char overload](ca1865-ca1867.md) | The char overload is a better performing overload for a string with a single char. |
| [CA1868: Unnecessary call to 'Contains' for sets](ca1868.md) | Both <xref:System.Collections.Generic.ISet`1.Add(`0)?displayProperty=nameWithType> and <xref:System.Collections.Generic.ICollection`1.Remove(`0)?displayProperty=nameWithType> perform a lookup, which makes it redundant to call <xref:System.Collections.Generic.ICollection`1.Contains(`0)?displayProperty=nameWithType> beforehand. It's more efficient to call <xref:System.Collections.Generic.ISet`1.Add(`0)> or <xref:System.Collections.Generic.ICollection`1.Remove(`0)> directly, which returns a Boolean value indicating whether the item was added or removed. |
| [CA1869: Cache and reuse 'JsonSerializerOptions' instances](ca1869.md) | Using a local instance of <xref:System.Text.Json.JsonSerializerOptions> for serialization or deserialization can substantially degrade the performance of your application if your code executes multiple times, since System.Text.Json internally caches serialization-related metadata into the provided instance. |
Expand Down
Loading