อาร์เรย์ใช้เก็บชุดข้อมูล จัดเก็บชุดสมาชิกขนาดคงที่ ชนิดเดียวกัน แบบลำดับ (sequential)
ไวยากรณ์
datatype[] arrayName;
รายละเอียด:
datatype- ชนิดข้อมูลของสมาชิก[]- อันดับของอาร์เรย์ (ระบุขนาด)arrayName- ชื่ออาร์เรย์
ตัวอย่าง
int[] goals;
ตัวอย่างเต็ม: ประกาศและ Initialize
using System;
namespace Demo {
class MyArray {
static void Main(string[] args) {
int[] goals = new int[5] {3, 2, 1, 5, 4};
int i, j;
for (j = 0; j < 5; j++) {
Console.WriteLine("Goals in FIFA - Match[{0}] = {1}", j, goals[j]);
}
Console.ReadKey();
}
}
}
ผลลัพธ์
Goals in FIFA - Match[0] = 3 Goals in FIFA - Match[1] = 2 Goals in FIFA - Match[2] = 1 Goals in FIFA - Match[3] = 5 Goals in FIFA - Match[4] = 4