C#idiomatic C#
how-to-initialize-a-dictionary-with-a-collection-initializercsharp/programming-guide/classes-and-structs

Language

How to initialize a dictionary with a collection initializer (C# Programming Guide)

Learn how to initialize a dictionary in C#, using either the Add method or an index initializer. This example shows both options.

  • page csharp/programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer
  • updated 10/14/2025

A System.Collections.Generic.Dictionary contains a collection of key/value pairs. Its System.Collections.Generic.Dictionary.Add method takes two parameters, one for the key and one for the value. One way to initialize a System.Collections.Generic.Dictionary, 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.

Example

In the following code example, a System.Collections.Generic.Dictionary 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:

sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/object-collection-initializers/HowToDictionaryInitializer.csHowToDictionaryInitializer

csharp
namespace object_collection_initializers;

// <SnippetHowToDictionaryInitializer>
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}");
        }
    }
}
// </SnippetHowToDictionaryInitializer>

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 System.Collections.Generic.Dictionary. 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.

Use AI to generate test data for dictionary collections

You can use AI tools, such as GitHub Copilot, to quickly generate dictionary test data and validation scenarios in your C# projects.

Here's an example prompt you can use in Visual Studio Copilot Chat.

copilot-prompt
Generate data collections for tests to create a separate Dictionary<int, Student> containing 10 valid Student records and 5 invalid records.
- Valid records should have realistic Name and Grade values.
- Invalid records should include cases such as missing Name, Grade < 0, or Grade > 100.
- This dictionary should be used only for testing purposes and not modify existing production code.
- Generate test code that utilizes this test data for validation scenarios.
- Call test method to run the test.

Review Copilot's suggestions before applying them.

For more information about GitHub Copilot, see GitHub's FAQs.

See also