C# Array - CopyTo() Method



The C# Array CopyTo() method copies all elements of the current one-dimensional array to another one-dimensional array starting at the specified index in the destination array.

According to the overloaded CopyTo method, the integer bit varies; You can pass a 32-bit integer index or a 64-bit integer index.

Exception

There are the following exceptions of CopyTo() methods −

  • ArgumentNullException: Thrown if the destination array is null.
  • ArgumentOutOfRangeException: Thrown if the index is less than zero.
  • ArgumentException Thrown if the destination array does not have enough space to accommodate the copied element or is of an incompatible type.

Syntax

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

public void CopyTo(Array array, int index);

Parameters

This method accepts the following parameters −

  • array: The destination array where the element will be copied.
  • index: The index value in the destination array at which copying begins.

Return value

This method does not return any value.

Example 1: Copy an Array of Int

This is the basic example of theCopyTo()method, Here we copy an array of integers −

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

      source.CopyTo(destination, 2);
      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

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

Example 2: Copy String into Another Array

Let us see another example of the CopyTo() method. Here, we copy an array of strings into another with a starting index 3 −

using System;
class Program {
   static void Main() {
      string[] source = { "apple", "banana", "cherry" };
      string[] destination = new string[5];

      source.CopyTo(destination, 1);
      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

, apple, banana, cherry, 

Example 3: Copy Characters to Another Array

In the previous example we have copied integer and string array, here we use the CopyTo() method to copy the character array −

using System;

class Program {
   static void Main() {
      char[] source = { 'A', 'B', 'C' };
      char[] destination = new char[6];

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

Output

Following is the output −

., ., ., A, B, C

Example 4: Handling Exception

This is another example of the CopyTo() method. Here, we are handling the exception and displaying the exception −

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

Output

Following is the output −

Exception: Destination array was not long enough. Check destIndex and length, and the array's lower bounds
Parameter name: destinationArray
csharp_array_class.htm
Advertisements