Pattern: Duplicate indexed element initialization
Issue: -
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.
Indexed element initializers in object initializers must initialize unique elements. A duplicate index will overwrite a previous element initialization.
Example of incorrect code:
var dictionary = new Dictionary<int, int>
{
[1] = 1, // CA2244
[2] = 2,
[1] = 3
};
Example of correct code:
var dictionary = new Dictionary<int, int>
{
[1] = 1,
[2] = 2,
};