Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
f33be5b
Step 1: Trim "What's New" article
BillWagner Dec 11, 2018
cfc2330
Add the YAML tutorial
BillWagner Dec 14, 2018
2b3f114
fix a YAML issue
BillWagner Dec 14, 2018
59a77c1
fix a build error
BillWagner Dec 14, 2018
9e3cf90
update interactive samples
BillWagner Dec 20, 2018
ad26a51
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
mairaw Dec 24, 2018
2b60a04
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
mairaw Dec 24, 2018
df08383
Update docs/csharp/tutorials/exploration/csharp-6.yml
mairaw Dec 24, 2018
367e630
Update docs/csharp/tutorials/exploration/csharp-6.yml
mairaw Dec 24, 2018
ca5169c
Update docs/csharp/tutorials/exploration/csharp-6.yml
mairaw Dec 24, 2018
da7d9c2
Update docs/csharp/tutorials/exploration/csharp-6.yml
mairaw Dec 24, 2018
d54055b
Update docs/csharp/tutorials/index.md
mairaw Dec 24, 2018
7390df8
Update docs/csharp/tutorials/exploration/csharp-6.yml
mairaw Dec 24, 2018
7c5b144
respond to feedback
BillWagner Dec 24, 2018
02413a6
fix tag name
BillWagner Dec 27, 2018
30f4451
fix build errors
BillWagner Dec 27, 2018
4d61846
fix build warnings, part II
BillWagner Dec 27, 2018
69df291
Update docs/csharp/programming-guide/classes-and-structs/how-to-initi…
Jan 4, 2019
b237a78
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
Jan 4, 2019
7d15f2d
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
Jan 4, 2019
bf11783
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
Jan 4, 2019
981d54b
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
Jan 4, 2019
9980687
Update docs/csharp/programming-guide/classes-and-structs/object-and-c…
Jan 4, 2019
3a82ceb
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
9f5dbf1
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
ed2af67
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
6e6f928
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
a9a215b
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
59497ca
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
f37c97a
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
d0df7e3
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
bb999e2
Update docs/csharp/tutorials/exploration/csharp-6.yml
Jan 4, 2019
71eed05
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
893fb9a
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
2d4171e
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
42903ad
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
8eee6ef
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
0a8058d
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
55986d4
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
a2378d6
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
3f82608
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
426d574
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
1e9a279
Update docs/csharp/whats-new/csharp-6.md
Jan 4, 2019
43ff86f
respond to feedback.
BillWagner Jan 4, 2019
96d46e2
merge feedback
BillWagner Jan 4, 2019
953a212
Update docs/csharp/whats-new/csharp-6.md
pkulikov Jan 4, 2019
0e78c9b
respond to feedback.
BillWagner Jan 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
---
title: "How to initialize a dictionary with a collection initializer - C# Programming Guide"
ms.custom: seodec18
ms.date: 07/20/2015
ms.date: 12/20/2018
helpviewer_keywords:
- "collection initializers [C#], with Dictionary"
ms.assetid: 25283922-f8ee-40dc-a639-fac30804ec71
---
# How to initialize a dictionary with a collection initializer (C# Programming Guide)

A <xref:System.Collections.Generic.Dictionary%602> contains a collection of key/value pairs. Its <xref:System.Collections.Generic.Dictionary%602.Add*> method takes two parameters, one for the key and one for the value. To initialize a <xref:System.Collections.Generic.Dictionary%602>, or any collection whose `Add` method takes multiple parameters, enclose each set of parameters in braces as shown in the following example.
A <xref:System.Collections.Generic.Dictionary%602> contains a collection of key/value pairs. Its <xref:System.Collections.Generic.Dictionary%602.Add*> method takes two parameters, one for the key and one for the value. One way to initialize a <xref:System.Collections.Generic.Dictionary%602>, 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 <xref:System.Collections.Generic.Dictionary%602> is initialized with instances of type `StudentName`.

[!code-csharp[csProgGuideLINQ#34](../../../csharp/programming-guide/arrays/codesnippet/CSharp/how-to-initialize-a-dictionary-with-a-collection-initializer_1.cs)]
In the following code example, a <xref:System.Collections.Generic.Dictionary%602> 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:

Note the two pairs of braces in each element of the collection. The innermost braces enclose the object initializer for the `StudentName`, and the outermost braces enclose the initializer for the key/value pair that will be added to the `students` <xref:System.Collections.Generic.Dictionary%602>. Finally, the whole collection initializer for the dictionary is enclosed in braces.
[!code-csharp-interactive[InitializerExample](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/object-collection-initializers/HowToDictionaryInitializer.cs#HowToDictionaryInitializer)]

## 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 [!INCLUDE[dnprdnshort](~/includes/dnprdnshort-md.md)], 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.
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 that will be added to the `students` <xref:System.Collections.Generic.Dictionary%602>. 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`.

## See also

Expand Down
Loading