TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Array.Copy Method

This C# example program uses the Array.Copy method. It copies all elements.

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 Method

Example. First, in this example 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.

Overload

Tip: Array.Copy allows you to copy the arrays without a for-loop, which avoids some complexity.

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

We create a new int array with 5 elements. Then, we allocate an empty array of 5 ints. Array.Copy 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.

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. Further on, we see exceptions you can get with Array.Copy.

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. This site has more information on using Lists with arrays.

Convert List to Array

Summary. We used 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: There are alternatives you can choose depending on your performance requirements.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf