new 키워드를 사용하여 배열의 인스턴스를 만듭니다. new 연산자는 개체를 만들거나 개체를 인스턴스화하는 데 사용됩니다. 여기 예제에서 new를 사용하여 클래스에 대한 개체가 생성됩니다.
다음은 예시입니다.
Calculate c = new Calculate();
new 키워드를 사용하여 배열의 인스턴스를 생성할 수도 있습니다.
double[] points = new double[10];
new 키워드는 컬렉션의 개체를 만드는 데에도 사용됩니다.
SortedList sl = new SortedList(); // SortedList List<string> myList = new List<string>() // List
예를 들어 보겠습니다.
예시
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);
}
}
} 출력
Destination Array ... 5 9 1 3