C#의 Clone() 메서드는 배열의 유사한 복사본을 만드는 데 사용됩니다.
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();
}
} 출력
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[];