Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 784 Bytes

File metadata and controls

34 lines (24 loc) · 784 Bytes

Pattern: Duplicate indexed element initialization

Issue: -

Description

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,
};

Further Reading