Skip to content

Overwrite Collection Element

Collection elements should not be replaced unconditionally

What does this mean ?

Collection elements should not be replaced unconditionally. It is highly suspicious when a value is saved for a key or index and then unconditionally overwritten.

What can happen ?

Such replacements are likely errors.

Recommendation

Collection elements should not be replaced unconditionally.

Sample Code

Vulnerable :

list[index] = "value 1";
list[index] = "value 2";  // Noncompliant

dictionary.Add(key, "value 1");
dictionary[key] = "value 2"; // Noncompliant

References