
You want to copy your typed array to another array using the C# language. The Array.Copy method is ideal for this purpose, but has some complexities. The Array type is an abstract base type from which all arrays are derived.
Here we look at the Array.Copy method overload that copies one source array to a target array. Both arrays must have at least the length specified in the third parameter. Array.Copy allows you to copy the arrays without a for loop, which avoids some complexity.
This C# example program uses the Array.Copy method. It copies all elements.
Example program for Array.Copy method [C#]
using System;
class Program
{
static void Main()
{
//
// Instantiate the source array.
//
int[] source = new int[5];
source[0] = 1;
source[1] = 2;
source[2] = 3;
source[3] = 4;
source[4] = 5;
//
// Instantiate and allocate the target array.
//
int[] target = new int[5];
//
// Copy the source to the target.
//
Array.Copy(source, target, 5);
//
// Display the target array.
//
Console.WriteLine("--- Target array ---");
foreach (int value in target)
{
Console.WriteLine(value);
}
}
}
Output
--- Target array ---
1
2
3
4
5Notes. The code first creates a new int array with five elements. Then, it allocates an empty array of five int elements. The Array.Copy method is called with the first parameter as the source array, the second parameter as the target array, and then the length we want to copy. Finally, the target array is written to the Console. It has the same contents as the source array.
Here we copy one range in an array to another range in the second array. This style of code is fraught with exceptions and you have to be very careful with checking the array bounds. Further on, we see all the exceptions you can get with Array.Copy. The example copies the first 3 elements from the source array to a smaller target array.
Example for copying array section [C#]
using System;
class Program
{
static void Main()
{
//
// Instantiate the source array.
//
int[] source = new int[5];
source[0] = 5;
source[1] = 4;
source[2] = 3;
source[3] = 2;
source[4] = 1;
//
// Instantiate the target.
//
int[] target = new int[3];
//
// Copy first three elements in source to target.
//
Array.Copy(source, 0, target, 0, 3);
//
// Display the result
//
Console.WriteLine("--- Destination array ---");
foreach (int value in target)
{
Console.WriteLine(value);
}
}
}
Output
--- Destination array ---
5
4
3
Here we look at some exceptions you will get when using Array.Copy. First, you cannot copy from or to a null reference array. This raises the ArgumentNullException. Second, each array must have adequate length for the complete copy operation. If either array is too short, there will be an ArgumentException.
ArgumentNullException ArgumentException NullReferenceException and Null Parameter
Looking into IL Disassembler, you will see that the Array.Copy overloads all call into the internal method Copy. This method is implemented in unmanaged code, and is not visible in the intermediate language.
IL Disassembler TutorialThe Array.Copy method is fast for most uses, and it is also simpler than manually copying all the elements. However, you can use the Buffer class to improve performance. The downside to the Buffer class is that you must specify byte sizes, which complicates your code. The Buffer.BlockCopy article benchmarks the Array.Copy method.
Buffer.BlockCopy Method
You can use the List generic class instead of Array to copy collections of elements. With the List class, you can copy into a new List simply by using the constructor. This site has more information on using Lists with arrays.
Convert List to Array
We saw how you can use the Array.Copy method in the C# language. This is a powerful method that can copy entire arrays or just sections of arrays. You must remember to allocate your arrays with adequate Length before calling Array.Copy. Finally, we reviewed some alternatives you can choose depending on your performance requirements.
Array Types