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


As the name suggests the Array.Copy() method in C# is used to copy elements of one array to another array.

The following is the syntax.

Array.Copy(src, dest, length);

Here

  • src = array to be copied

  • dest = destination array

  • length = how many elements to copy

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

Example

 Live Demo

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

Output

Destination Array ...
99
66
111
33

Updated on: 23-Jun-2020

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements