C# Programming Guide
How to: Initialize a Dictionary with a Collection Initializer (C# Programming Guide)

Updated: July 2009

A Dictionary<(Of <(TKey, TValue>)>) contains a collection of key/value pairs. Its Add method takes two parameters, one for the key and one for the value. To initialize a Dictionary<(Of <(TKey, TValue>)>), or any collection whose Add method takes multiple parameters, enclose each set of parameters in braces as shown in the following example.

Example

In the following code example, a Dictionary<(Of <(TKey, TValue>)>) is initialized with instances of type Student.

C#
Dictionary<int, StudentName> 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}}
};

Note the two pairs of braces in each element of the collection. The innermost braces enclose the object initializer for the Student, and the outermost braces enclose the initializer for the key/value pair that will be added to the students Dictionary<(Of <(TKey, TValue>)>). Finally, the whole collection initializer for the dictionary is enclosed in braces.

Compiling the Code

To run this code, copy and paste the class into a Visual C# console application project that has been created in Visual Studio. By default, this project targets version 3.5 of the .NET Framework, and it has a reference to System.Core.dll and a using directive for System.Linq. If one or more of these requirements are missing from the project, you can add them manually. For more information, see How to: Create a LINQ Project.

See Also

Concepts

Reference

Change History

Date

History

Reason

July 2009

Updated the example's comment section.

Customer feedback.

Tags :


Community Content

bbulkow
this does not seem to work on .NET 2.0
Although the page claims "specificity to .NET 3.5", there is no claim as to whether it works in 3.0 or 2.0. It doesn't appear to work in .NET 2.0's C#. Could someone clarify exactly which version this works for, and if there's any way to statically initialize a Collection in earlier versions?
Tags :

bitbonk
re: this does not seem to work on .NET 2.0
This feature (collection initialization) requires the C# 3.0 compiler wich was introduced with Visual Studio 2008 and only works with .NET 3.5 or later. Static initialization only works for arrays in previous versions.
Generally in the MSDN documents features are always only available for the version listed in that rectangle in the upleft corner (
"This page is specific to ... / Other versions are also available for the following: " ). If a version is not listed there it will not work for this version.
Tags :

Kamron Batman
Syntax problems?
I am attemping to use this method with Dictionary< int, int[]>, and it has lines that look roughly like this:

{ 101, new int[] { 1, 2, 3, 4 } }

And of course its not working when I am initializing it in a class as a public static. It gives me a syntax error on the first line, saying that it expects a terminator. I have a feeling that this feature is semi-broken in .NET Framework 3.5 for specific situations like that. Oddly enough though, visual studio can compile it, so I am not sure what to make of it.

EDIT: I was using framework 2.0 - This works in 3.5.
Tags :

Page view tracker