0Pricing
C# Academy · Lesson

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,6

Mixing 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,5

All lessons in this course

  1. Collection Expression Syntax
  2. The Spread Element
  3. Target Typing Collections
  4. Collection Builders
← Back to C# Academy