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
Merge two arrays using C# AddRange() method
The AddRange() method in C# is used to add elements from a collection to the end of a List<T>. This method is particularly useful for merging arrays by first converting them to a list, adding both arrays using AddRange(), and then converting the result back to an array.
Syntax
Following is the syntax for the AddRange() method −
public void AddRange(IEnumerable<T> collection)
Parameters
collection − The collection whose elements should be added to the end of the List<T>. The collection itself cannot be null, but it can contain elements that are null.
Using AddRange() to Merge Arrays
To merge two arrays using AddRange(), create a new List<T>, add both arrays using the method, and convert the result to an array using ToArray() −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
int[] arr1 = { 15, 20, 27, 56 };
int[] arr2 = { 62, 69, 76, 92 };
// displaying array1
Console.WriteLine("Array 1...");
foreach(int ele in arr1) {
Console.WriteLine(ele);
}
// displaying array2
Console.WriteLine("Array 2...");
foreach(int ele in arr2) {
Console.WriteLine(ele);
}
// merge arrays using AddRange()
var myList = new List<int>();
myList.AddRange(arr1);
myList.AddRange(arr2);
// convert back to array
int[] arr3 = myList.ToArray();
Console.WriteLine("Merged array..");
foreach (int res in arr3) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Array 1... 15 20 27 56 Array 2... 62 69 76 92 Merged array.. 15 20 27 56 62 69 76 92
Using AddRange() with Different Data Types
The AddRange() method works with any data type. Here's an example with string arrays −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
string[] names1 = { "Alice", "Bob", "Charlie" };
string[] names2 = { "David", "Eve", "Frank" };
var namesList = new List<string>();
namesList.AddRange(names1);
namesList.AddRange(names2);
string[] mergedNames = namesList.ToArray();
Console.WriteLine("Merged string array:");
foreach (string name in mergedNames) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Merged string array: Alice Bob Charlie David Eve Frank
Alternative Methods for Merging Arrays
| Method | Description | Performance |
|---|---|---|
| AddRange() | Uses List<T> to merge arrays | Good for multiple arrays |
| Concat() | LINQ method for concatenation | Lazy evaluation, memory efficient |
| Array.Copy() | Copies elements directly to new array | Best performance for large arrays |
Conclusion
The AddRange() method provides a simple and readable way to merge arrays in C#. It works by creating a List<T>, adding multiple collections using AddRange(), and converting the result back to an array with ToArray(). This approach is particularly useful when you need to merge multiple arrays or collections of the same type.
