Skip to content

Overwrite Collection Element

The replacement of collection pieces should not be done unconditionally.

What does this mean ?

The replacement of collection pieces should not be done unconditionally. When a value is stored for a key or index and then unconditionally rewritten, it is very suspicious.

What can happen ?

Such substitutions are almost certainly errors.

Recommendation

It is not advisable to change all collection items at the same time.

Sample Code

Vulnerable :

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

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

References