C#

C#

Made by DeepSource

Consider using the indexer when adding a key to Dictionary<K, V> CS-R1119

Anti-pattern
Major
Autofix

A dictionary's indexer updates an element if it already exists and adds it if it doesn't. This is an easier and shorter way of adding/updating a key and should be the preferred way.

Bad Practice

if (dict.ContainsKey(k, v))
{
    dict[k] = v;
}
else
{
    dict.Add(k, v);
}

Recommended

dict[k] = v;