System.ArrayCopyTo() and System.ArrayClone() in C#

PHPz
Release: 2023-09-01 09:41:10
forward
1304 people have browsed it

C# 中的 System.ArrayCopyTo() 与 System.ArrayClone()

The ArrayCopyTo() method copies all elements of the current one-dimensional array to the specified one-dimensional array, starting from the specified target array index. The index is specified as a 32-bit integer.

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

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

Example

The ArrayCopyTo() method copies all elements of the current one-dimensional array to the specified one-dimensional array, starting from the specified target array index. Indexes are specified as 32-bit integers.

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

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

Example

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);
      }
   }
}
Copy after login

Array.Clone() method in C# Clone array. Here we have an array of strings −

Example

using System;

class Program {
   static void Main() {
      string[] arr = { "one", "two", "three", "four", "five" };
      string[] arrCloned = arr.Clone() as string[];

      Console.WriteLine(string.Join(",", arr));

      // cloned array
      Console.WriteLine(string.Join(",", arrCloned));
      Console.WriteLine();
   }
}
Copy after login

The above is the detailed content of System.ArrayCopyTo() and System.ArrayClone() in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template