Introduction
In this article, you will learn that the Array is a collection of similar data types stored in contiguous memory locations. It is a proper data structure that allows you to store and manipulate values of the same type. You will learn copy array method used to copy a section of one Array to another array in C#, and it performs type casting and boxing as required to data in C#.
To create an array in C#, you need to specify the data type of the array elements and the number of elements it can hold. Here's an example of how to declare an integer array that can have five elements,
int[] myArray = new int[5];
This creates an array called myArray that can hold five integers. You can also initialize the Array with values like this.

Methods to copy an array in C#
- Using the ArrayCopy()
- Using the Clone()
- Using the CopyTo()
Method 1. Using the ArrayCopy()
The array copy method copies all the current array elements to the specified destination array in C#. This method should be called from the source array and takes two parameters. The first is the Array you want to copy to, and the second parameter tells it what index of the destination array it should start copying into in C#
public class ArrayDemo {
public static void Main() {
int[] n = new int[10]; /* n is an array of 10 integers */
int[] m = new int[5]; /* m is an array of 5 integers */
for (int i = 0; i < 10; i++) {
n[i] = i + 200;
}
Console.WriteLine("Original Array...");
foreach(int j in n) {
int i = j - 200;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
Array.Copy(n, 0, m, 0, 5);
Console.WriteLine("New Array...");
foreach(int res in m) {
Console.WriteLine(res);
}
Console.ReadKey();
}
}
This statement is declared two integer arrays: n With a length of 10 and m size of 5. Using a loop, it fills the
Array with values ranging from 200 to 209. After filling the n The code prints out the original Array by looping through each element of the Array using a foreach loop. It subtracts 200 from each value and displays the index and value of the array element.
Next, the code uses the Array.Copy method to copy the first five elements of the n Array to the m Array. The Array.Copy method takes the source array (n), the starting index in the source array (0), the destination array (m), the starting index in the destination array (0), and the number of elements to copy (5). Finally, the code prints out the Array's contents using a foreach loop without subtracting 200 from the values.
When run, the output of the program will be

Method 2. Using the Clone()
The Array class provides a Clone method allowing you to create a new array with the same contents as an existing one. Here's an example of how to use the Clone method:
public static void Main()
{
int[] originalArray = { 1, 2, 3, 4, 5 };
// create a new array using the Clone method
int[] clonedArray = (int[])originalArray.Clone();
// print the original array
Console.WriteLine("Original array:");
PrintArray(originalArray);
// print the cloned array
Console.WriteLine("Cloned array:");
PrintArray(clonedArray);
// modify an element in the original array
originalArray[0] = 100;
// print the original array again
Console.WriteLine("Modified original array:");
PrintArray(originalArray);
// print the cloned array again
Console.WriteLine("Cloned array after modifying original array:");
PrintArray(clonedArray);
Console.ReadLine();
}
// helper method to print an array
static void PrintArray(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}
}


Join the conversation! Your thoughts help the community grow.