Skip to content

Commit a8c1705

Browse files
Add examples of a spread element to the "Collection initializers" article (#44045)
* Add example of spread operator for collection initializers * Inlcude the spread element example in the text * Include additional syntax for manually creating additional element * Include spread element in full example code * Remove unnecessary System.Xml.Linq
1 parent 3c2efb6 commit a8c1705

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ You can specify [null](../../language-reference/keywords/null.md) as an element
149149

150150
:::code language="csharp" source="./snippets/object-collection-initializers/BasicObjectInitializers.cs" id="ListInitializerWithNull":::
151151

152+
You can use a spread element to create one list that copies other list or lists.
153+
154+
:::code language="csharp" source="./snippets/object-collection-initializers/BasicObjectInitializers.cs" id="ListInitializerWithSpreadOperator":::
155+
156+
And include additonal elements along with using a spread element.
157+
158+
:::code language="csharp" source="./snippets/object-collection-initializers/BasicObjectInitializers.cs" id="ListInitializerWithSpreadOperatorAndAdditionalElement":::
159+
152160
You can specify indexed elements if the collection supports read / write indexing.
153161

154162
:::code language="csharp" source="./snippets/object-collection-initializers/BasicObjectInitializers.cs" id="DictionaryIndexerInitializer":::

docs/csharp/programming-guide/classes-and-structs/snippets/object-collection-initializers/BasicObjectInitializers.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ from p in products
9898
};
9999
// </SnippetListInitializerWithNull>
100100

101+
// <SnippetListInitializerWithSpreadOperator>
102+
List<Cat> allCats = [.. cats, .. moreCats];
103+
// </SnippetListInitializerWithSpreadOperator>
104+
105+
// <SnippetListInitializerWithSpreadOperatorAndAdditionalElement>
106+
List<Cat> additionalCats = [.. cats, new Cat { Name = "Furrytail", Age = 5 }, .. moreCats];
107+
// </SnippetListInitializerWithSpreadOperatorAndAdditionalElement>
108+
101109
// <SnippetDictionaryIndexerInitializer>
102110
var numbers = new Dictionary<int, string>
103111
{
@@ -182,15 +190,10 @@ public static void Main()
182190
null
183191
};
184192

185-
// Display results.
186-
System.Console.WriteLine(cat.Name);
193+
List<Cat> allCats = [.. cats, new Cat { Name = "Łapka", Age = 5 }, cat, .. moreCats];
187194

188-
foreach (Cat c in cats)
189-
{
190-
System.Console.WriteLine(c.Name);
191-
}
192-
193-
foreach (Cat? c in moreCats)
195+
// Display results.
196+
foreach (Cat? c in allCats)
194197
{
195198
if (c != null)
196199
{
@@ -203,13 +206,14 @@ public static void Main()
203206
}
204207
}
205208
// Output:
206-
//Fluffy
207-
//Sylvester
208-
//Whiskers
209-
//Sasha
210-
//Furrytail
211-
//Peaches
212-
//List element has null value.
209+
// Sylvester
210+
// Whiskers
211+
// Sasha
212+
// Łapka
213+
// Fluffy
214+
// Furrytail
215+
// Peaches
216+
// List element has null value.
213217
}
214218
// </SnippetFullExample>
215219

0 commit comments

Comments
 (0)