How to use the Copy(, ,) method of array class in C#

The Array.Copy() method in C# is used to copy elements from one array to another. This method provides a fast and efficient way to duplicate array elements without manually iterating through each element.

Syntax

Following is the basic syntax for the three-parameter overload −

Array.Copy(sourceArray, destinationArray, length);

Parameters

  • sourceArray − The array from which elements are copied

  • destinationArray − The array to which elements are copied

  • length − The number of elements to copy from the source array

The method copies elements starting from index 0 of the source array to index 0 of the destination array.

Array.Copy() Process Source Array 99 66 111 33 Copy 4 elements Destination Array 99 66 111 33 [0] [1] [2] [3] [0] [1] [2] [3]

Using Array.Copy() to Copy All Elements

Example

using System;

class Program {
   static void Main() {
      int[] sourceArray = new int[4];
      sourceArray[0] = 99;
      sourceArray[1] = 66;
      sourceArray[2] = 111;
      sourceArray[3] = 33;
      
      int[] destinationArray = new int[4];
      Array.Copy(sourceArray, destinationArray, 4);
      
      Console.WriteLine("Source Array:");
      foreach (int value in sourceArray) {
         Console.WriteLine(value);
      }
      
      Console.WriteLine("Destination Array:");
      foreach (int value in destinationArray) {
         Console.WriteLine(value);
      }
   }
}

The output of the above code is −

Source Array:
99
66
111
33
Destination Array:
99
66
111
33

Using Array.Copy() to Copy Partial Elements

Example

using System;

class Program {
   static void Main() {
      string[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
      string[] selectedFruits = new string[5];
      
      // Copy only first 3 elements
      Array.Copy(fruits, selectedFruits, 3);
      
      Console.WriteLine("Original Array:");
      foreach (string fruit in fruits) {
         Console.WriteLine(fruit);
      }
      
      Console.WriteLine("\nCopied Array (first 3 elements):");
      foreach (string fruit in selectedFruits) {
         Console.WriteLine(fruit ?? "null");
      }
   }
}

The output of the above code is −

Original Array:
Apple
Banana
Cherry
Date
Elderberry

Copied Array (first 3 elements):
Apple
Banana
Cherry
null
null

Using Array.Copy() with Different Array Types

Example

using System;

class Program {
   static void Main() {
      double[] temperatures = {23.5, 18.2, 30.1, 15.8};
      double[] backupTemperatures = new double[4];
      
      Array.Copy(temperatures, backupTemperatures, temperatures.Length);
      
      // Modify original array
      temperatures[0] = 25.0;
      
      Console.WriteLine("Original temperatures (after modification):");
      foreach (double temp in temperatures) {
         Console.WriteLine(temp + "°C");
      }
      
      Console.WriteLine("\nBackup temperatures:");
      foreach (double temp in backupTemperatures) {
         Console.WriteLine(temp + "°C");
      }
   }
}

The output of the above code is −

Original temperatures (after modification):
25°C
18.2°C
30.1°C
15.8°C

Backup temperatures:
23.5°C
18.2°C
30.1°C
15.8°C

Key Rules

  • The destination array must have enough space to hold the copied elements

  • Both arrays must be of compatible types

  • The method performs a shallow copy for reference types

  • If the length parameter exceeds the available elements in source or destination, an exception is thrown

Conclusion

The Array.Copy() method provides an efficient way to copy elements between arrays in C#. It supports copying all or partial elements, works with various data types, and is much faster than manual element-by-element copying for large arrays.

Updated on: 2026-03-17T07:04:35+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements