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 or concatenate two lists in C#?
In C#, there are multiple ways to join or concatenate two lists. The most common approaches include using the AddRange() method, LINQ's Concat() method, and the Union() method. Each method has different behaviors and use cases.
Syntax
Following is the syntax for using AddRange() to concatenate lists −
list1.AddRange(list2);
Following is the syntax for using LINQ Concat() method −
var result = list1.Concat(list2).ToList();
Following is the syntax for using Union() to join lists without duplicates −
var result = list1.Union(list2).ToList();
Using AddRange() Method
The AddRange() method modifies the original list by adding all elements from the second list to the first list −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
var products1 = new List<string>();
products1.Add("Belts");
products1.Add("Tshirt");
products1.Add("Trousers");
var products2 = new List<string>();
products2.Add("Footwear");
products2.Add("Electronics");
Console.WriteLine("List 1:");
foreach(var p in products1) {
Console.WriteLine(p);
}
Console.WriteLine("\nList 2:");
foreach(var p in products2) {
Console.WriteLine(p);
}
products1.AddRange(products2);
Console.WriteLine("\nConcatenated list:");
foreach(var p in products1) {
Console.WriteLine(p);
}
}
}
The output of the above code is −
List 1: Belts Tshirt Trousers List 2: Footwear Electronics Concatenated list: Belts Tshirt Trousers Footwear Electronics
Using LINQ Concat() Method
The Concat() method creates a new list without modifying the original lists −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
var numbers1 = new List<int> { 1, 2, 3 };
var numbers2 = new List<int> { 4, 5, 6 };
var concatenated = numbers1.Concat(numbers2).ToList();
Console.WriteLine("Original List 1: " + string.Join(", ", numbers1));
Console.WriteLine("Original List 2: " + string.Join(", ", numbers2));
Console.WriteLine("Concatenated: " + string.Join(", ", concatenated));
}
}
The output of the above code is −
Original List 1: 1, 2, 3 Original List 2: 4, 5, 6 Concatenated: 1, 2, 3, 4, 5, 6
Using Union() Method for Unique Elements
The Union() method concatenates lists while removing duplicate elements −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
var list1 = new List<string> { "Apple", "Banana", "Orange" };
var list2 = new List<string> { "Banana", "Grape", "Apple", "Mango" };
var unionResult = list1.Union(list2).ToList();
Console.WriteLine("List 1: " + string.Join(", ", list1));
Console.WriteLine("List 2: " + string.Join(", ", list2));
Console.WriteLine("Union (no duplicates): " + string.Join(", ", unionResult));
}
}
The output of the above code is −
List 1: Apple, Banana, Orange List 2: Banana, Grape, Apple, Mango Union (no duplicates): Apple, Banana, Orange, Grape, Mango
Comparison of Methods
| Method | Modifies Original | Handles Duplicates | Performance |
|---|---|---|---|
| AddRange() | Yes (modifies first list) | Keeps all duplicates | Fast |
| Concat() | No (creates new list) | Keeps all duplicates | Medium |
| Union() | No (creates new list) | Removes duplicates | Slower |
Conclusion
Use AddRange() when you want to modify the original list, Concat() when you need a new combined list, and Union() when you want to eliminate duplicates. Choose the method based on whether you need to preserve original lists and handle duplicate elements.
