dotnet / dotnet/docs

Additional "When to suppress warnings" should be demonstrated. CA1853 matching is too loose.

Open
#44,193 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

### Type of issue

Incomplete list of reasons to suppress this warning.

### Description

The only item under "When to suppress warnings" is "It's safe to suppress a warning if performance isn't a concern." That's not the only reason.

It's worthwhile to check a Dictionary collection for .ContainsKey() before a .Remove() of that item if we're planning to alter the order of the Dictionary collection by adding the updated item at the end on identical match.

Example:
```
// "seed" is generated as an int earlier
Dictionary a = [];
// ... population of "a" from other code ...
if (a.ContainsKey(seed))
{
a.Remove(seed);
Dictionary a2 = [a];
// alternatively: Dictionary a2 = []; for (int i = 0; i < a.Count; i++) { a2.Add(a.ElementAt(i).Key, a.ElementAt(i).Value); }
a.Clear();
a = a2;
}
a.Add(seed, object[])
```

### Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1853

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1853.md

### Document Version Independent Id

e01a0926-70a5-177f-017f-46b93b6c1463

### Article author

@gewarren

### Metadata

* ID: 4fc29d18-6855-2bf4-b53c-856c4cde4499
* Service: **dotnet-fundamentals**

[Related Issues](https://github.com/dotnet/docs/issues?q=is%3Aissue+is%3Aopen+e01a0926-70a5-177f-017f-46b93b6c1463)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.