C#
Dictionary
collection initializer
programming
.NET

Can I use a collection initializer for DictionaryTKey, TValue entries?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, you can initialize a dictionary with collection-initializer syntax in C#. In fact, there are two closely related forms: the classic Add-style entry syntax and the newer indexer syntax, and they behave differently when duplicate keys appear.

Classic Dictionary Collection Initializer

The traditional syntax uses nested braces for each key-value pair:

csharp
1using System;
2using System.Collections.Generic;
3
4var capitals = new Dictionary<string, string>
5{
6    { "FR", "Paris" },
7    { "JP", "Tokyo" },
8    { "CA", "Ottawa" }
9};
10
11Console.WriteLine(capitals["JP"]);

This works because collection initializers call the target collection’s Add method behind the scenes. For Dictionary<TKey, TValue>, that means each entry is added as if you had written capitals.Add("FR", "Paris").

Indexer Initializer Syntax

Newer C# versions also support indexer syntax:

csharp
1using System;
2using System.Collections.Generic;
3
4var capitals = new Dictionary<string, string>
5{
6    ["FR"] = "Paris",
7    ["JP"] = "Tokyo",
8    ["CA"] = "Ottawa"
9};
10
11Console.WriteLine(capitals["CA"]);

This form does not call Add. It assigns through the dictionary indexer instead.

That distinction matters because indexer assignment updates an existing key, while Add throws if the key already exists.

Duplicate Keys Behave Differently

Classic initializer with duplicate keys:

csharp
1var data = new Dictionary<string, int>
2{
3    { "x", 1 },
4    { "x", 2 }
5};

That throws an exception, because the second entry is effectively another Add.

Indexer initializer with duplicate keys:

csharp
1var data = new Dictionary<string, int>
2{
3    ["x"] = 1,
4    ["x"] = 2
5};
6
7Console.WriteLine(data["x"]);

This leaves the dictionary with the value 2 for "x", because the second assignment overwrites the first.

So the answer is not only “yes, you can.” It is also “choose the initializer form that matches your duplicate-key policy.”

When Each Form Is Appropriate

Use the classic brace-pair form when:

  • you want duplicate keys to fail fast
  • you want behavior identical to Add
  • the dictionary should be treated like a fixed set of unique entries

Use indexer syntax when:

  • overwriting is acceptable
  • you prefer the visual style
  • you are merging values and later entries should win

Both are valid. The better choice depends on the semantics you want, not on syntax alone.

Works with Other Dictionary-Like Types Too

The same idea extends to other types that expose the required members. Collection initializers are not limited to Dictionary<TKey, TValue> specifically. The compiler looks for the shape it needs, such as Add, and indexer initializers work when an indexer setter exists.

That is why custom collection types can participate too if their API fits the language rules.

Readability Considerations

For small fixed maps, collection initializers are usually the cleanest option. They keep declaration and setup in one place:

csharp
1var httpCodes = new Dictionary<int, string>
2{
3    [200] = "OK",
4    [404] = "Not Found",
5    [500] = "Server Error"
6};

For large dynamic dictionaries built from loops, configuration files, or conditional logic, plain method calls may still read better than forcing everything into an initializer.

Common Pitfalls

The biggest mistake is assuming both initializer forms behave the same for duplicate keys. They do not.

Another issue is forgetting that collection initializers require the right underlying members. The syntax looks special, but it still relies on real methods or indexers implemented by the target type.

Developers also sometimes use the classic brace-pair syntax and then get surprised by an exception during object construction. If duplicate data is possible, either validate it first or choose indexer syntax intentionally.

Finally, do not make the initializer so large that readability collapses. Concise syntax helps only while the data remains easy to scan.

Summary

  • Yes, dictionaries support collection initializer syntax in C#.
  • '{ key, value } entries use Add under the hood.'
  • '[key] = value entries use the indexer setter instead.'
  • Duplicate keys throw with Add syntax but overwrite with indexer syntax.
  • Pick the form that matches the behavior you actually want.

Course illustration
Course illustration

All Rights Reserved.