C#에서 복제는 배열을 복제하려는 경우에 유용합니다. C#의 Clone() 메서드는 배열의 유사한 복사본을 만드는 데 사용됩니다. C#에는 Clone 메서드와 ICloneable 인터페이스가 있습니다.
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));
Console.WriteLine(string.Join(",", arrCloned));
Console.WriteLine();
}
} 출력
one,two,three,four,five one,two,three,four,five
위에 문자열 배열이 있습니다 -
string[] arr = { "one", "two", "three", "four", "five" }; 이를 통해 새 문자열 배열에서 "as" 연산자와 함께 Clone() 메서드를 사용하여 배열을 복사했습니다 -
string[] arrCloned = arr.Clone() as string[];