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
Combine two arrays in C#
Combining two arrays in C# can be accomplished using various methods. The most common approaches include using List.AddRange() method, Array.Copy(), or LINQ's Concat() method.
Syntax
Using List.AddRange() method −
var list = new List<T>(); list.AddRange(array1); list.AddRange(array2); T[] combinedArray = list.ToArray();
Using Array.Copy() method −
T[] combinedArray = new T[array1.Length + array2.Length]; Array.Copy(array1, 0, combinedArray, 0, array1.Length); Array.Copy(array2, 0, combinedArray, array1.Length, array2.Length);
Using LINQ Concat() method −
T[] combinedArray = array1.Concat(array2).ToArray();
Using List.AddRange() Method
This approach creates a List, adds both arrays using AddRange(), then converts back to an array −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
int[] arr1 = { 37, 45, 65 };
int[] arr2 = { 70, 89, 118 };
Console.WriteLine("Array 1: " + string.Join(", ", arr1));
Console.WriteLine("Array 2: " + string.Join(", ", arr2));
var myList = new List<int>();
myList.AddRange(arr1);
myList.AddRange(arr2);
int[] combinedArray = myList.ToArray();
Console.WriteLine("Combined Array: " + string.Join(", ", combinedArray));
}
}
The output of the above code is −
Array 1: 37, 45, 65 Array 2: 70, 89, 118 Combined Array: 37, 45, 65, 70, 89, 118
Using Array.Copy() Method
This method creates a new array with the exact size needed and copies elements directly −
using System;
class Demo {
static void Main() {
string[] fruits1 = { "Apple", "Banana" };
string[] fruits2 = { "Orange", "Mango", "Grape" };
Console.WriteLine("First Array: " + string.Join(", ", fruits1));
Console.WriteLine("Second Array: " + string.Join(", ", fruits2));
string[] combinedFruits = new string[fruits1.Length + fruits2.Length];
Array.Copy(fruits1, 0, combinedFruits, 0, fruits1.Length);
Array.Copy(fruits2, 0, combinedFruits, fruits1.Length, fruits2.Length);
Console.WriteLine("Combined Array: " + string.Join(", ", combinedFruits));
}
}
The output of the above code is −
First Array: Apple, Banana Second Array: Orange, Mango, Grape Combined Array: Apple, Banana, Orange, Mango, Grape
Using LINQ Concat() Method
LINQ provides the most concise approach using the Concat() extension method −
using System;
using System.Linq;
class Demo {
static void Main() {
double[] numbers1 = { 1.5, 2.7, 3.9 };
double[] numbers2 = { 4.1, 5.3 };
Console.WriteLine("Array 1: " + string.Join(", ", numbers1));
Console.WriteLine("Array 2: " + string.Join(", ", numbers2));
double[] combinedNumbers = numbers1.Concat(numbers2).ToArray();
Console.WriteLine("Combined Array: " + string.Join(", ", combinedNumbers));
}
}
The output of the above code is −
Array 1: 1.5, 2.7, 3.9 Array 2: 4.1, 5.3 Combined Array: 1.5, 2.7, 3.9, 4.1, 5.3
Performance Comparison
| Method | Performance | Memory Usage | Best For |
|---|---|---|---|
| Array.Copy() | Fastest | Lowest | Large arrays, performance-critical code |
| List.AddRange() | Moderate | Higher | General use, readable code |
| LINQ Concat() | Slowest | Moderate | Small arrays, functional programming style |
Conclusion
C# offers multiple ways to combine arrays: Array.Copy() for best performance, List.AddRange() for general use, and LINQ's Concat() for concise code. Choose the method based on your specific performance requirements and coding preferences.
