Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to join two lists in C#?
To join two lists in C#, you can use several methods. The most common approaches are using the AddRange() method to modify an existing list, or using LINQ's Concat() method to create a new combined list without modifying the originals.
Syntax
Using AddRange() to modify the first list −
list1.AddRange(list2);
Using LINQ Concat() to create a new list −
var combinedList = list1.Concat(list2).ToList();
Using AddRange() Method
The AddRange() method adds all elements from the second list to the end of the first list, modifying the original list −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
var list1 = new List<string>();
list1.Add("Keyboard");
list1.Add("Mouse");
Console.WriteLine("Our list1....");
foreach(var item in list1) {
Console.WriteLine(item);
}
var list2 = new List<string>();
list2.Add("Hard Disk");
list2.Add("Pen Drive");
Console.WriteLine("Our list2....");
foreach(var item in list2) {
Console.WriteLine(item);
}
list1.AddRange(list2);
Console.WriteLine("Concatenated list....");
foreach(var item in list1) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Our list1.... Keyboard Mouse Our list2.... Hard Disk Pen Drive Concatenated list.... Keyboard Mouse Hard Disk Pen Drive
Using LINQ Concat() Method
The Concat() method creates a new list containing elements from both lists without modifying the original lists −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
var list1 = new List<string> { "Apple", "Banana" };
var list2 = new List<string> { "Cherry", "Date" };
Console.WriteLine("Original list1:");
foreach(var item in list1) {
Console.WriteLine(item);
}
Console.WriteLine("Original list2:");
foreach(var item in list2) {
Console.WriteLine(item);
}
var combinedList = list1.Concat(list2).ToList();
Console.WriteLine("Combined list:");
foreach(var item in combinedList) {
Console.WriteLine(item);
}
Console.WriteLine("list1 remains unchanged: " + list1.Count + " items");
Console.WriteLine("list2 remains unchanged: " + list2.Count + " items");
}
}
The output of the above code is −
Original list1: Apple Banana Original list2: Cherry Date Combined list: Apple Banana Cherry Date list1 remains unchanged: 2 items list2 remains unchanged: 2 items
Comparison of Methods
| Method | Modifies Original | Performance | Use Case |
|---|---|---|---|
| AddRange() | Yes (modifies first list) | Faster (no new list created) | When you want to extend an existing list |
| Concat() | No (creates new list) | Slower (creates new list) | When you need to preserve original lists |
Joining Lists with Different Types
You can also join lists of different but compatible types using LINQ −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
var numbers = new List<int> { 1, 2, 3 };
var strings = new List<string> { "4", "5", "6" };
var combined = numbers.Select(n => n.ToString()).Concat(strings).ToList();
Console.WriteLine("Combined list:");
foreach(var item in combined) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Combined list: 1 2 3 4 5 6
Conclusion
To join two lists in C#, use AddRange() to modify an existing list or Concat() to create a new combined list. Choose AddRange() for better performance when you don't need to preserve the original lists, and Concat() when you want to keep the original lists unchanged.
