There are four common ways to combine two lists in C#: using Add() while looping, using Concat(), using AddRange(), and using ForEach() with Add(). Here’s how to use all four techniques.
Combine two lists with Add() and a loop
The first method is to use a foreach loop using the source list (the list you’re reading from), and calling .Add() on the destination list, passing in each value from the source list.
In the code below, we pass the values from the “first” and “second” list variables into the “combined” list variable with the loops in lines 7-10 and 12-15.
On lines 20-23, we use a “foreach” and Add() to add all the values from the “second” list variable into the “first” list variable.
List<string> first = new List<string> { "a", "b", "c" }; List<string> second = new List<string> { "d", "e", "f" }; // Add items from "first" and "second" lists into a new "combined" variable List<string> combined = new List<string>(); foreach (string val in first) { combined.Add(val); } foreach (string val in second) { combined.Add(val); } Console.WriteLine(combined); // To add items from the "second" list into the "first" list foreach (string val in second) { first.Add(val); } Console.WriteLine(first);
Combine two lists with Concat()
In this code, on line 5, we read the values from the “first” list variable, concatenate the values from the “second” list variable, and populate all those values into the “combined” list variable.
Be aware that the Concat() function return IEnumerable<T>, not List<T>. So, if you want to populate the results in the concatenation into a List variable, you need to add .ToList() after the Concat() – to cast the results into List<T>.
List<string> first = new List<string> { "a", "b", "c" }; List<string> second = new List<string> { "d", "e", "f" }; // Add items from "first" and "second" lists into a new "combined" variable List<string> combined = first.Concat(second).ToList(); Console.WriteLine(combined); Console.WriteLine(first);
Combine two lists with AddRange()
If you want to put the values from one List into an existing List, you can use the AddRange() function.
On the destination List variable, in this code, the “first” variable, call the AddRange() function, passing in the List variable that contains the values you want to add to the destination List variable – in this code, that’s the “second” variable.
Unlike the Concat() function, AddRange() does modify the List variable it’s called on.
List<string> first = new List<string> { "a", "b", "c" }; List<string> second = new List<string> { "d", "e", "f" }; // To add items from "second" list into "first" list first.AddRange(second); Console.WriteLine(first);
Combine two lists with ForEach() and Add()
Where AddRange() pulled values from the “second” variable, ForEach() pushes values from the “second” variable.
The code on line 5 says to take each value from the “second” variable, that’s the ForEach(), put the value into the “s” variable that’s to the left of the “=>”, the use that “s” variable in the code to the right of the “=>”. In this case, the code calls “first.Add(s)”, which add the value from the “s” variable into the “first” variable.
This code is basically the equivalent to lines 20-23 of the code from the first example – looping with a ForEach() and calling Add().
List<string> first = new List<string> { "a", "b", "c" }; List<string> second = new List<string> { "d", "e", "f" }; // To add items from "second" list into "first" list second.ForEach(s => first.Add(s)); Console.WriteLine(first); Console.WriteLine(second);
How I typically combine two lists in C#
To combine multiple lists into a new variable, I typically use the Concat() method.
To add values from a list into an existing list variable, I typically use the AddRange() method.