The Spread Element
Combine collections with the spread operator.
The Spread Element ..
Inside a collection expression, the spread element .. inlines the contents of another collection. It is how you combine sequences declaratively.
using System;
int[] a = [1, 2, 3];
int[] b = [4, 5, 6];
int[] all = [..a, ..b];
Console.WriteLine(string.Join(",", all)); // 1,2,3,4,5,6Mixing Spreads and Single Elements
You can freely interleave spread elements with individual values, and the order is preserved.
using System;
int[] middle = [2, 3, 4];
int[] result = [1, ..middle, 5];
Console.WriteLine(string.Join(",", result)); // 1,2,3,4,5All lessons in this course
- Collection Expression Syntax
- The Spread Element
- Target Typing Collections
- Collection Builders