You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "How to initialize a dictionary with a collection initializer"
3
3
description: Learn how to initialize a dictionary in C#, using either the Add method or an index initializer. This example shows both options.
A <xref:System.Collections.Generic.Dictionary%602> contains a collection of key/value pairs. Its <xref:System.Collections.Generic.Dictionary%602.Add%2A> 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.
15
15
16
16
> [!NOTE]
17
-
> The major difference between these two ways of initializing the collection is that in case of having duplicated keys, for example:
17
+
> The major difference between these two ways of initializing the collection is how duplicated keys are handled, for example:
><xref:System.Collections.Generic.Dictionary%602.Add%2A>methodwillthrow<xref:System.ArgumentException>: `'An item with the same key has already been added. Key: 111'`,
><xref:System.Collections.Generic.Dictionary%602.Add%2A>methodthrows<xref:System.ArgumentException>: `'An item with the same key has already been added. Key: 111'`,
0 commit comments