ArrayCopyTo() 메서드는 현재 1차원 Array의 모든 요소를 지정된 대상 Array 인덱스에서 시작하여 지정된 1차원 Array에 복사합니다. 인덱스는 32비트 정수로 지정됩니다.
C#의 CopyTo() 메서드는 한 배열의 요소를 다른 배열로 복사하는 데 사용됩니다. 이 방법에서는 원본 배열에서 복사하려는 시작 인덱스를 설정할 수 있습니다.
다음은 C#에서 배열 클래스의 CopyTo(,) 메소드 사용법을 보여주는 예입니다 -
예시
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);
}
}
} C#의 Array.Clone() 메서드는 배열을 복제합니다. 여기에 문자열 배열이 있습니다 -
예시
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();
}
}