C# Array.Copy Method: source and destinationArray

This C# example program uses the Array.Copy method. It uses sourceArray and destinationArray.
Array.Copy copies elements from one array to another. It has some complexities. This operation can result in certain exceptions. The type of elements—in both the target and source arrays—is important. This is a static method.Static
First example. Here we use the Array.Copy method overload that copies one source array to a destination array. Both arrays must have at least the length specified in the third parameter.Overload

First: We create a new int array with 5 elements. Then, we allocate an empty array of 5 ints.

Copy: The first parameter is the source array. The second parameter is the destination array. And then we pass the length we want to copy.

Finally: The target array is written to the Console. It has the same contents as the source array.

C# program that uses Array.Copy method 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 5
Example 2. 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 careful with checking the array bounds.

Next: The example copies the first 3 elements from the source array to a smaller target array.

C# program that copies array section 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
Exceptions. Here we look at Array.Copy exceptions. 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.ArgumentNullExceptionNullReferenceException

Caution: If either array is too short, there will be an ArgumentException. Check your arguments.

Internals. 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 Tutorial
Performance. The Array.Copy method is fast for most uses. It is simpler than manually copying all the elements. The Buffer class improves performance. The downside to Buffer is that you must specify byte sizes, which is complex.Buffer
List. 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.Convert List, Array
Summary. We used the Array.Copy method. 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.Array
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls