배열을 선언해도 메모리의 배열은 초기화되지 않습니다. 배열 변수가 초기화되면 배열에 값을 할당할 수 있습니다.
다음은 선언이며 배열을 생성하지 않습니다 -
int[] id;
다음은 정수 배열을 만듭니다. 배열은 참조 유형이므로 배열의 인스턴스를 생성하려면 new 키워드를 사용해야 합니다 -
Int[] id = new int[5] {}; 예를 들어 보겠습니다 -
예시
using System;
namespace ArrayApplication {
public class MyArray {
public static void Main(string[] args) {
int [] n = new int[5];
int i,j;
/* initialize elements of array n */
for ( i = 0; i < 5; i++ ) {
n[ i ] = i + 10;
}
/* output each array element's value */
for (j = 0; j < 5; j++ ) {
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
}
}
} 출력
Element[0] = 10 Element[1] = 11 Element[2] = 12 Element[3] = 13 Element[4] = 14