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 append a second list to an existing list in C#?
Use the AddRange() method to append a second list to an existing list in C#. The AddRange() method adds all elements from one collection to the end of another list, effectively combining two lists into one.
Syntax
Following is the syntax for using AddRange() method −
list1.AddRange(list2);
Parameters
collection − The collection whose elements should be added to the end of the List. The collection itself cannot be null, but it can contain elements that are null.
Using AddRange() Method
The AddRange() method modifies the original list by adding all elements from the second list to its end −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> list1 = new List<string>();
list1.Add("One");
list1.Add("Two");
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
Console.WriteLine("Second list...");
List<string> list2 = new List<string>();
list2.Add("Three");
list2.Add("Four");
foreach(string value in list2) {
Console.WriteLine(value);
}
Console.WriteLine("After Append...");
list1.AddRange(list2);
foreach(string value in list1) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
First list... One Two Second list... Three Four After Append... One Two Three Four
Using AddRange() with Different Data Types
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<int> numbers1 = new List<int> { 1, 2, 3 };
List<int> numbers2 = new List<int> { 4, 5, 6 };
Console.WriteLine("Numbers1 before AddRange:");
Console.WriteLine(string.Join(", ", numbers1));
Console.WriteLine("Numbers2:");
Console.WriteLine(string.Join(", ", numbers2));
numbers1.AddRange(numbers2);
Console.WriteLine("Numbers1 after AddRange:");
Console.WriteLine(string.Join(", ", numbers1));
Console.WriteLine("Count: " + numbers1.Count);
}
}
The output of the above code is −
Numbers1 before AddRange: 1, 2, 3 Numbers2: 4, 5, 6 Numbers1 after AddRange: 1, 2, 3, 4, 5, 6 Count: 6
Using AddRange() with Arrays
You can also use AddRange() to append an array to a list −
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> fruits = new List<string> { "Apple", "Banana" };
string[] moreFruits = { "Orange", "Mango", "Grape" };
Console.WriteLine("Original list:");
Console.WriteLine(string.Join(", ", fruits));
fruits.AddRange(moreFruits);
Console.WriteLine("After adding array:");
Console.WriteLine(string.Join(", ", fruits));
}
}
The output of the above code is −
Original list: Apple, Banana After adding array: Apple, Banana, Orange, Mango, Grape
Conclusion
The AddRange() method is the most efficient way to append one list to another in C#. It modifies the original list by adding all elements from the source collection to its end, and can work with lists, arrays, or any IEnumerable collection.
