C#에서 배열을 선언하려면 다음 구문을 사용할 수 있습니다. -
datatype[ ] Name_of_array;
여기,
-
데이터 유형 배열의 요소 유형을 지정하는 데 사용됩니다.
-
[ ] 배열의 크기를 지정합니다.
-
이름_of_array 배열의 이름을 지정합니다.
다음은 예입니다 -
double[ ] balance;
배열이 선언되고 값이 추가되는 예를 살펴보겠습니다. −
예
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [ ] n = new int[10]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100;
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
} 출력
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109