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
-18Lines changed: 0 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,6 @@ ms.date: 02/12/2025
5
5
helpviewer_keywords:
6
6
- "collection initializers [C#], with Dictionary"
7
7
ms.topic: how-to
8
-
ms.custom: copilot-scenario-highlight
9
8
ms.assetid: 25283922-f8ee-40dc-a639-fac30804ec71
10
9
---
11
10
# How to initialize a dictionary with a collection initializer (C# Programming Guide)
@@ -23,9 +22,6 @@ A <xref:System.Collections.Generic.Dictionary%602> contains a collection of key/
23
22
><xref:System.Collections.Generic.Dictionary%602.Add%2A>methodthrows<xref:System.ArgumentException>: `'An item with the same key has already been added. Key: 111'`,
@@ -34,20 +30,6 @@ In the following code example, a <xref:System.Collections.Generic.Dictionary%602
34
30
35
31
Notethetwopairsofbracesineachelementofthecollectioninthefirstdeclaration. Theinnermostbracesenclosetheobjectinitializerforthe `StudentName`, andtheoutermostbracesenclosetheinitializerfor the key/value pair to 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`.
36
32
37
-
## Use AI to initialize a dictionary
38
-
39
-
You can use AI tools, such as GitHub Copilot, to generate C# code to initialize a dictionary with a collection initializer. You can customize the prompt to add specifics per your requirements.
40
-
41
-
The following text shows an example prompt for Copilot Chat:
42
-
43
-
```copilot-prompt
44
-
Generate C# code to initialize Dictionary<int, Employee> using key-value pairs within the collection initializer. The employee class is a record class with two properties: Name and Age.
45
-
```
46
-
47
-
GitHub Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs).
48
-
49
33
## See also
50
34
51
35
- [Object and Collection Initializers](./object-and-collection-initializers.md)
0 commit comments