C#의 Array.ConstrainedCopy() 메서드는 지정된 소스 인덱스에서 시작하는 배열의 요소 범위를 복사하고 지정된 대상 인덱스에서 시작하는 다른 배열에 붙여넣는 데 사용됩니다.
구문
public static void ConstrainedCopy (Array sourceArr, int sourceIndex, Array destinationArr, int destinationIndex, int length);
여기,
-
sourceArr - 복사할 데이터가 포함된 배열입니다.
-
sourceIndex - 복사가 시작되는 sourceArr의 인덱스를 나타내는 32비트 정수입니다.
-
destinationArr - 데이터를 수신하는 배열입니다.
-
destinationIndex - 저장이 시작되는 destinationArr의 인덱스를 나타내는 32비트 정수입니다.
-
len - 복사할 요소의 수를 나타내는 32비트 정수입니다.
이제 Array.ConstrainedCopy() 메서드를 구현하는 예를 살펴보겠습니다. -
예시
using System;
public class Demo{
public static void Main(){
int[] arrDest = new int[10];
Console.WriteLine("Array elements...");
int[] arrSrc = { 20, 50, 100, 150, 200, 300, 400};
for (int i = 0; i < arrSrc.Length; i++){
Console.Write("{0} ", arrSrc[i]);
}
Console.WriteLine();
Array.ConstrainedCopy(arrSrc, 3, arrDest, 0, 4);
Console.WriteLine("Destination Array: ");
for (int i = 0; i < arrDest.Length; i++){
Console.Write("{0} ", arrDest[i]);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Array elements... 20 50 100 150 200 300 400 Destination Array: 150 200 300 400 0 0 0 0 0 0