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
+21-1Lines changed: 21 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
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.
4
-
ms.date: 02/12/2025
4
+
ms.date: 10/14/2025
5
5
helpviewer_keywords:
6
6
- "collection initializers [C#], with Dictionary"
7
7
ms.topic: how-to
8
8
ms.assetid: 25283922-f8ee-40dc-a639-fac30804ec71
9
+
ms.custom: copilot-scenario-highlight
9
10
---
10
11
# How to initialize a dictionary with a collection initializer (C# Programming Guide)
11
12
@@ -30,6 +31,25 @@ In the following code example, a <xref:System.Collections.Generic.Dictionary%602
30
31
31
32
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`.
32
33
34
+
## Use AI to generate test data for dictionary collections
35
+
36
+
You can use AI tools, such as GitHub Copilot, to quickly generate dictionary test data and validation scenarios in your C# projects.
37
+
38
+
Here's an example prompt you can use in Visual Studio Copilot Chat.
39
+
40
+
```copilot-prompt
41
+
Generate data collections for tests to create a separate Dictionary<int, Student> containing 10 valid Student records and 5 invalid records.
42
+
- Valid records should have realistic Name and Grade values.
43
+
- Invalid records should include cases such as missing Name, Grade < 0, or Grade > 100.
44
+
- This dictionary should be used only for testing purposes and not modify existing production code.
45
+
- Generate test code that utilizes this test data for validation scenarios.
46
+
- Call test method to run the test.
47
+
```
48
+
49
+
Review Copilot's suggestions before applying them.
50
+
51
+
For more information about GitHub Copilot, see GitHub's [FAQs](https://github.com/features/copilot#faq).
52
+
33
53
## See also
34
54
35
55
- [Object and Collection Initializers](./object-and-collection-initializers.md)
0 commit comments