The Spread Element
Combine collections with the spread operator.
The Spread Element is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
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,5Prepending and Appending
Spreads make "add to the front" or "add to the back" trivial, producing a new collection rather than mutating the original.
using System;
int[] core = [10, 20, 30];
int[] withHead = [0, ..core];
int[] withTail = [..core, 40];
Console.WriteLine(string.Join(",", withHead)); // 0,10,20,30
Console.WriteLine(string.Join(",", withTail)); // 10,20,30,40Spreading Multiple Sources
Any number of collections can be spread into one. This concatenates several lists in a single expression.
using System;
using System.Collections.Generic;
List<string> x = ["a", "b"];
List<string> y = ["c"];
List<string> z = ["d", "e"];
List<string> merged = [..x, ..y, ..z];
Console.WriteLine(string.Join(",", merged)); // a,b,c,d,eSpreading Different Collection Types
The spread source can be any enumerable: an array into a list, a list into an array, and so on. The target type controls the result.
using System;
using System.Collections.Generic;
int[] arr = [1, 2];
List<int> list = [3, 4];
int[] combined = [..arr, ..list];
Console.WriteLine(string.Join(",", combined)); // 1,2,3,4Spreading IEnumerable
Even lazy sequences from LINQ can be spread; they are enumerated when the new collection is built.
using System;
using System.Linq;
var evens = Enumerable.Range(1, 6).Where(n => n % 2 == 0);
int[] result = [0, ..evens];
Console.WriteLine(string.Join(",", result)); // 0,2,4,6Building Up Collections
Spreads are great for incremental construction without imperative Add calls.
using System;
int[] baseline = [1, 2, 3];
int[] extra = [4, 5];
int[] full = [..baseline, ..extra, 6];
Console.WriteLine(full.Length); // 6Order Is Significant
The resulting sequence follows the textual order of elements and spreads, so swapping spreads changes the output.
using System;
int[] a = [1, 2];
int[] b = [3, 4];
Console.WriteLine(string.Join(",", [..a, ..b])); // 1,2,3,4
Console.WriteLine(string.Join(",", [..b, ..a])); // 3,4,1,2Spreads vs Concat
Spreads often replace Concat + ToArray. They read more clearly and let the compiler size the result efficiently.
using System;
using System.Linq;
int[] a = [1, 2];
int[] b = [3, 4];
// old
int[] oldWay = a.Concat(b).ToArray();
// new
int[] newWay = [..a, ..b];
Console.WriteLine(oldWay.SequenceEqual(newWay)); // TrueSpreading into Spans
Because the target can be a span, you can combine data with potential stack allocation for performance-sensitive paths.
using System;
ReadOnlySpan<int> head = [1, 2];
ReadOnlySpan<int> tail = [3, 4];
int[] joined = [..head, ..tail];
Console.WriteLine(string.Join(",", joined)); // 1,2,3,4A Practical Merge
Here we merge default settings with overrides in one expression, keeping later values after the defaults.
using System;
string[] defaults = ["--verbose", "--color"];
string[] userArgs = ["--output=log.txt"];
string[] finalArgs = [..defaults, ..userArgs];
Console.WriteLine(string.Join(" ", finalArgs));Quick Check
Verify your understanding of the spread element.
Recap
You learned the spread element .. inside collection expressions.
[..a, ..b]concatenates collections.- Spreads mix with single elements and preserve order.
- Sources can be arrays, lists, spans, or any
IEnumerable. - It is a clearer alternative to
Concat().ToArray().
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “The Spread Element” lesson free?
Yes — the full text of “The Spread Element” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “The Spread Element”?
Combine collections with the spread operator. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “The Spread Element” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Collection Expression Syntax
- The Spread Element
- Target Typing Collections
- Collection Builders