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 concatenate Two Arrays in C#?
To concatenate two arrays in C#, you can use several methods depending on your requirements. The most common approaches include using the Concat() method from LINQ, copying elements manually, or using Array.Copy().
Syntax
Using LINQ Concat() method −
var result = array1.Concat(array2).ToArray();
Using Array.Copy() method −
Array.Copy(sourceArray, destinationArray, length);
Using LINQ Concat() Method
The Concat() method from LINQ is the most straightforward way to concatenate arrays. It creates a new array containing elements from both arrays −
using System;
using System.Linq;
class Program {
static void Main() {
string[] array1 = { "Hello", "World" };
string[] array2 = { "from", "C#" };
string[] result = array1.Concat(array2).ToArray();
Console.WriteLine("Concatenated Array:");
foreach (string item in result) {
Console.Write(item + " ");
}
}
}
The output of the above code is −
Concatenated Array: Hello World from C#
Using Array.Copy() Method
For better performance with large arrays, you can use Array.Copy() to manually copy elements into a new array −
using System;
class Program {
static void Main() {
int[] array1 = { 1, 2, 3 };
int[] array2 = { 4, 5, 6, 7 };
int[] result = new int[array1.Length + array2.Length];
Array.Copy(array1, 0, result, 0, array1.Length);
Array.Copy(array2, 0, result, array1.Length, array2.Length);
Console.WriteLine("Concatenated Array:");
foreach (int num in result) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Concatenated Array: 1 2 3 4 5 6 7
Using string.Join() for String Arrays
When working with string arrays and you want to create a single concatenated string, string.Join() is the most efficient approach −
using System;
class Program {
static void Main() {
string[] array1 = { "C#", "is" };
string[] array2 = { "powerful", "language" };
string result1 = string.Join(" ", array1);
string result2 = string.Join(" ", array2);
string finalResult = result1 + " " + result2;
Console.WriteLine("Joined String: " + finalResult);
// Alternative: Join arrays first, then convert to string
string[] combined = array1.Concat(array2).ToArray();
string directJoin = string.Join(" ", combined);
Console.WriteLine("Direct Join: " + directJoin);
}
}
The output of the above code is −
Joined String: C# is powerful language Direct Join: C# is powerful language
Comparison of Methods
| Method | Best For | Performance |
|---|---|---|
| LINQ Concat() | Small to medium arrays, readability | Good |
| Array.Copy() | Large arrays, performance-critical code | Excellent |
| string.Join() | Converting arrays to single string | Excellent for strings |
Conclusion
Array concatenation in C# can be achieved using Concat() for simplicity, Array.Copy() for performance, or string.Join() when creating concatenated strings. Choose the method based on your specific requirements for performance and output format.
