C# Join Two Lists Together
-
C# Program to Join Two
Lists
Together UsingAddRange()
Method -
C# Program to Join Two
Lists
Together UsingEnumerable.Concat()
Method -
C# Program to Join Two
Lists
Together Usingforeach
Loop Method

The List
is a data structure to store and process data. We can perform various useful operations on Lists
.
In C#, there are multiple methods to add items to Lists
, remove items from Lists
and various other operations. But in some cases, we need to join already existing Lists
together.
In this article, we are going to discuss some methods by which we can join two existing Lists
together. Let’s check out these methods.
C# Program to Join Two Lists
Together Using AddRange()
Method
The method AddRange()
is used to add two Lists
. It adds the second List
to the first List
. The List
to be added is passed as a parameter to this method.
The correct syntax to use this method is as follows:
AnyList.AddRange(ListToAdd);
Example Code:
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> Fruits = new List<string>() { "Apple", "Banana", "Orange", "Mango" };
List<string> Vegetables = new List<string>() { "Potato", "Tomato", "Cauli Flower", "Onion" };
Fruits.AddRange(Vegetables);
Console.Write("Fruits and Vegetables are: ");
Console.WriteLine(String.Join(",", Fruits));
}
}
Output:
Fruits and Vegetables are: Apple,Banana,Orange,Mango,Potato,Tomato,Cauli Flower,Onion
C# Program to Join Two Lists
Together Using Enumerable.Concat()
Method
The method Enumerable.Concat()
is a LINQ
class method. It is used to concatenate two Lists
together. The List
that is to be added is passed as a parameter to this method.
The correct syntax to use this method is as follows:
AnyList.Concat(ListToAdd);
Example Code:
using System;
using System.Linq;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> Fruits = new List<string>() { "Apple", "Banana", "Orange", "Mango" };
List<string> Vegetables = new List<string>() { "Potato", "Tomato", "Cauli Flower", "Onion" };
List<string> FruitsAndVegetables = Fruits.Concat(Vegetables).ToList();
Console.Write("Fruits and Vegetables are: ");
Console.WriteLine(String.Join(",", FruitsAndVegetables));
}
}
Output:
Fruits and Vegetables are: Apple,Banana,Orange,Mango,Potato,Tomato,Cauli Flower,Onion
C# Program to Join Two Lists
Together Using foreach
Loop Method
The foreach
loop method is a traditional method, using which we can perform any operation on any data structure or data type. In this case, we have used a foreach
loop to concatenate two Lists
together. The foreach
loop will add items one by one in each iteration.
The correct syntax to use foreach
loop to add two Lists
is as follows:
ListToAdd.foreach(AnyList => AnyList.Add(AnyList));
Example Code:
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> Fruits = new List<string>() { "Apple", "Banana", "Orange", "Mango" };
List<string> Vegetables = new List<string>() { "Potato", "Tomato", "Cauli Flower", "Onion" };
Vegetables.ForEach(Fruit => Fruits.Add(Fruit));
Console.Write("Fruits and Vegetables are: ");
Console.WriteLine(String.Join(",", Fruits));
}
}
Output:
Fruits and Vegetables are: Apple,Banana,Orange,Mango,Potato,Tomato,Cauli Flower,Onion