How to initialize a dictionary with a collection initializer (C# Programming Guide)
A Dictionary<TKey,TValue> contains a collection of key/value pairs. Its Add method takes two parameters, one for the key and one for the value. One way to initialize a Dictionary<TKey,TValue>, or any collection whose Add
method takes multiple parameters, is to enclose each set of parameters in braces as shown in the following example. Another option is to use an index initializer, also shown in the following example.
Note
The major difference between these two ways of initializing the collection is that in case of having duplicated keys, for example:
{ 111, new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 } },
{ 111, new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } },
Add method will throw ArgumentException: 'An item with the same key has already been added. Key: 111'
,
while the second part of example, the public read / write indexer method, will quietly overwrite the already existing entry with the same key.
Example
In the following code example, a Dictionary<TKey,TValue> is initialized with instances of type StudentName
. The first initialization uses the Add
method with two arguments. The compiler generates a call to Add
for each of the pairs of int
keys and StudentName
values. The second uses a public read / write indexer method of the Dictionary
class:
public class HowToDictionaryInitializer
{
class StudentName
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int ID { get; set; }
}
public static void Main()
{
var students = new Dictionary<int, StudentName>()
{
{ 111, new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 } },
{ 112, new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } },
{ 113, new StudentName { FirstName="Andy", LastName="Ruth", ID=198 } }
};
foreach(var index in Enumerable.Range(111, 3))
{
Console.WriteLine($"Student {index} is {students[index].FirstName} {students[index].LastName}");
}
Console.WriteLine();
var students2 = new Dictionary<int, StudentName>()
{
[111] = new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 },
[112] = new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } ,
[113] = new StudentName { FirstName="Andy", LastName="Ruth", ID=198 }
};
foreach (var index in Enumerable.Range(111, 3))
{
Console.WriteLine($"Student {index} is {students2[index].FirstName} {students2[index].LastName}");
}
}
}
Note the two pairs of braces in each element of the collection in the first declaration. The innermost braces enclose the object initializer for the StudentName
, and the outermost braces enclose the initializer for the key/value pair to be added to the students
Dictionary<TKey,TValue>. Finally, the whole collection initializer for the dictionary is enclosed in braces. In the second initialization, the left side of the assignment is the key and the right side is the value, using an object initializer for StudentName
.