Alternative solution to fix the issue
- Dominant language
- No language data
- Stars
- 4.8k
- Forks
- 6.1k
- Avg merge
- 15h 21m
- Merged PRs (30d)
- 370
Description
### Type of issue
Missing information
### Description
I more often replace a
```
if(!dictionary.ContainsKey(2)) {
dictionary.Add(2, "Hello World");
}
```
by a
```
dictionary[2] = "Hello World";
```
than a
```
dictionary.TryAdd(2, "Hello World");
```
This changes the behavior from first-wins to last-wins, but often that can be ignored e.g. as the expected behavior is not defined etc.
Maybe that alternative solution could also be mentioned in the description somewhere or even provides as alternative refactoring option.
### Page URL
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1864
### Content source URL
https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1864.md
### Document Version Independent Id
ff5bb003-dfd0-173b-6a4f-34012dd5f94d
### Article author
@gewarren
### Metadata
* ID: 78f3e28a-0823-42ea-d5d8-b1625b49babc
* Service: **dotnet-fundamentals**
[Related Issues](https://github.com/dotnet/docs/issues?q=is%3Aissue+is%3Aopen+ff5bb003-dfd0-173b-6a4f-34012dd5f94d)
Contributor guide
Assessment
This issue has not been assessed yet.