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

CsharpProgrammingServer Side Programming

The CopyTo() method in C# is used to copy elements of one array to another array. In this method, you can set the starting index from where you want to copy from the source array.

The following is the syntax.

CopyTo(dest, index);

Here dest = destination array

index= starting index

The following is an example showing the usage of CopyTo(,) method of array class in C#.

Example

 Live Demo

using System;
class Program {
   static void Main() {
      int[] arrSource = new int[4];
      arrSource[0] = 5;
      arrSource[1] = 9;
      arrSource[2] = 1;
      arrSource[3] = 3;
      int[] arrTarget = new int[4];
      // CopyTo() method
      arrSource.CopyTo(arrTarget,0 );
      Console.WriteLine("Destination Array ...");
      foreach (int value in arrTarget) {
         Console.WriteLine(value);
      }
   }
}

Output

Destination Array ...
5
9
1
3
raja
Published on 18-Sep-2018 12:56:36
Advertisements