C# Array - Copy() Method



The C# Array Copy() method copies a range of elements from one array to another array and performs typecasting and boxing as required.

The process of assigning a value of one data type to another data type is called typecasting and the process of converting a value type to its reference type is called boxing.

Let's see Array Copy in C#

Exception

There are the following exceptions of Copy() method −

  • ArgumentNullException: Thrown if either sourceArray and destinationArray is null.
  • RankException: Thrown if the array have different number of dimensions.
  • ArrayTypeMismatchException: Thrown if the type of sourceArray does not compatible with destinationArray.
  • ArgumentOutOfRangeException: Thrown if sourceIndex, destinationIndex, or length is out of range.
  • ArgumentException Thrown if the range to copy is invalid.

Syntax

Following is the syntax of the C# Array Copy() method −

public static void Copy(Array sourceArray, Array destinationArray, int length);

Parameters

This method accepts the following parameters −

  • sourceArray: The one-dimensional array that contains data to be copy.
  • destinationArray: The one-dimensional array that is destination of the elements copied from sourceArray.
  • length: The number of element to copy.

Return value

This method does not return any value.

Example 1: Partial Copy

Let us crate a basic example, how we use the Copy() method in the C# code −

using System;
class Program {
   public static void Main() {
      int[] source = { 1, 2, 3, 4, 5 };
      int[] destination = { 10, 20, 30, 40, 50 };

      Array.Copy(source, destination, 3);
      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

1, 2, 3, 40, 50

Example 2: Default Copy

Let us see another example of the Copy() method. Here, we copy all elements of source array to destination array −

using System;
class Program {
   public static void Main() {
      int[] source = { 1, 2, 3, 4, 5 };
      int[] destination = new int[5];

      Array.Copy(source, destination, source.Length);
      Console.WriteLine(string.Join(", ", source));
      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

1, 2, 3, 4, 5
1, 2, 3, 4, 5

Example 3: Copy Between Array of Different Sizes

In this example, we use the Copy() method to transfer elements from the source array to the destination array. If the size of the source array is smaller than that of the destination array, the remaining indices in the destination array will be filled with zeroes after the source array has been copied −

using System;
class Program {
   static void Main() {
      int[] source = { 1, 2, 3, 4, 5, 6, 7 };
      int[] destination = new int[10];

      Array.Copy(source, destination, source.Length);

      Console.WriteLine("Destination array after copying:");
      foreach (int item in destination) {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Destination array after copying:
1 2 3 4 5 6 7 0 0 0 

Example 4: Handling Error

This is another example of the copy() method. Here, we are handling the error and displaying the error message −

using System;
class Program {
   static void Main() {
      try {
         int[] source = { 1, 2, 3 };
         int[] destination = new int[2];	     
         Array.Copy(source, destination, 5);
      }
      catch (ArgumentException ex) {
         Console.WriteLine($"Error: {ex.Message}");
      }
   }
}

Output

Following is the output −

ERROR!
Error: length
csharp_array_class.htm
Advertisements