Skip to main content

How to Append One Sequence to Another with LINQ Concat in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

morning.Concat(afternoon) gives you one sequence: every item of morning, then every item of afternoon. Neither array changes, and duplicates stay.

Microsoft's reference: Concatenates two sequences. What you get back is An IEnumerable<T> that contains the concatenated elements of the two input sequences.

Syntax​

Syntax
IEnumerable<T> <all> = <first>.Concat(<second>);
PartWhat it is
<first>The sequence whose items come first, such as an array or a list
<second>The sequence whose items come after them, with the same item type T
<all>A new sequence. <first> and <second> stay as they were

The Example​

A console app called LinqConcat joins the 2 morning lessons and the 3 afternoon lessons of a school day into one sequence, and prints them.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n LinqConcat
cd LinqConcat
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/LinqConcat/LinqConcat.csproj:
Determining projects to restore...
Restored /Users/you/source/LinqConcat/LinqConcat.csproj (in 43 ms).
Restore succeeded.

dotnet new console -n LinqConcat creates a folder named LinqConcat with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or LinqConcat.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the two arrays:

Program.cs (the lessons)
string[] morning = ["Math", "Science"];
string[] afternoon = ["Art", "Music", "Math"];

Then join them and print every lesson:

Program.cs (joining them)
// Concat gives every item of morning, then every item of afternoon.
IEnumerable<string> wholeDay = morning.Concat(afternoon);

foreach (string lesson in wholeDay)
{
Console.WriteLine(lesson);
}

Console.WriteLine($"Lessons today: {wholeDay.Count()}");
Console.WriteLine($"Lessons in morning: {morning.Length}");
  • morning.Concat(afternoon) returns a new sequence, wholeDay, with the morning lessons first.
  • foreach goes through wholeDay one lesson at a time.
  • wholeDay.Count() counts the joined lessons, and morning.Length is the size of the morning array on its own.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Math
Science
Art
Music
Math
Lessons today: 5
Lessons in morning: 2

macOS-style terminal window titled Terminal — LinqConcat showing dotnet run printing Math, Science, Art, Music and Math, then Lessons today: 5 and Lessons in morning: 2

All 5 lessons came out, the morning ones first, and Math appears twice because Concat keeps every item. The morning array still holds its 2 lessons: Concat built a new sequence and changed neither array.

The Complete File​

macOS-style code window titled Program.cs — LinqConcat showing a morning array of two lessons, an afternoon array of three, a wholeDay sequence made with morning.Concat(afternoon), a foreach loop that prints each lesson, and two lines that print the joined count and the morning array's length

Program.cs
string[] morning = ["Math", "Science"];
string[] afternoon = ["Art", "Music", "Math"];

// Concat gives every item of morning, then every item of afternoon.
IEnumerable<string> wholeDay = morning.Concat(afternoon);

foreach (string lesson in wholeDay)
{
Console.WriteLine(lesson);
}

Console.WriteLine($"Lessons today: {wholeDay.Count()}");
Console.WriteLine($"Lessons in morning: {morning.Length}");

Common Mistakes​

MistakeWhat happensDo this instead
Calling Concat without using its resultNothing changes: a lessons list of 2 still held 2 after lessons.Concat(afternoon);, while the sequence that Concat returned held 4Store the result in a variable, as wholeDay does. To add the items to the list itself, use the list's AddRange method
A second sequence that can be nullThe build warned warning CS8604: Possible null reference argument for parameter 'second', and the call threw ArgumentNullException: Value cannot be null. (Parameter 'second')Pass an empty collection instead of null: morning.Concat(extra ?? []) gave the 2 morning lessons
Expecting duplicates to be removedConcat kept both Math lessons. The reference: The Union method returns only unique elements.Use Union when each item should appear once: it gave Math, Science, Art, Music

FAQ​

Does Concat copy the items?​

No. The reference: This method is implemented by using deferred execution. Concat reads the two sequences only when you go through its result. A check added Music to an afternoon list after calling Concat: the Concat result included Music, but a copy made with ToList() before the change didn't.

How do I join more than two sequences?​

Call Concat again on the result. morning.Concat(afternoon).Concat(evening) gave Math, Science, Art, Music, Chess, in that order.

Conclusion​

first.Concat(second) gives one sequence with every item of first followed by every item of second, duplicates included. It changes neither sequence and reads them only when you go through the result, so store the result, and call ToList() when you need a copy that won't change.

Additional Resources​