How to join two lists in C# and VB.NET

To join two lists in C# and VB.NET you can use one of the following snippet.

Samples C#

//INIT
var listOne = new List<int>() { 1, 2, 3, 4, 5 };
var listTwo = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

//using Concat, this does not modify listOne. listThree includes all values, even the duplicates.
var listThree = listOne.Concat(listTwo).ToList();

//using Union, this does not modify listOne. listFour includes all values, without duplicates.
var listFour = listOne.Union(listTwo).ToList();

//using AddRange, this modifies listOne. listOne includes all values, even the duplicates.
listOne.AddRange(listTwo);

Samples VB.NET

'INIT
Dim listOne = New List(Of Integer)() From { 1, 2, 3, 4, 5 }
Dim listTwo = New List(Of Integer)() From { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

'using Concat, this does not modify listOne. listThree includes all values, even the duplicates.
Dim listThree = listOne.Concat(listTwo).ToList()

'using Union, this does not modify listOne. listFour includes all values, without duplicates.
Dim listFour = listOne.Union(listTwo).ToList()

'using AddRange, this modifies listOne. listOne includes all values, even the duplicates.
listOne.AddRange(listTwo)

5 thoughts on “How to join two lists in C# and VB.NET

Leave a Reply