Mảng dùng để lưu trữ tập hợp dữ liệu, có thể xem như tập hợp các biến cùng kiểu lưu tại các vị trí bộ nhớ liền kề.
Khai báo và khởi tạo:
// Khai báo
int[] runs = new int[10];
// Khai báo + khởi tạo giá trị ngay
int[] runs = new int[5] { 125, 173, 190, 264, 188 };
Ví dụ hoàn chỉnh
using System;
namespace Program
{
class Demo
{
static void Main(string[] args)
{
int[] runs = new int[5] { 125, 173, 190, 264, 188 };
int i, j;
for (j = 0; j < 5; j++)
{
Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]);
}
Console.ReadKey();
}
}
}
Kết quả
Innings score of Cricketer[0] = 125
Innings score of Cricketer[1] = 173
Innings score of Cricketer[2] = 190
Innings score of Cricketer[3] = 264
Innings score of Cricketer[4] = 188
Đặc điểm: Index base-0, kích thước cố định sau khi khởi tạo, truy cập O(1), duyệt qua for hoặc foreach.