Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Cách tạo mảng động (Dynamic Array) trong C#

Mảng động (dynamic arrays) có thể mở rộng, ưu hơn mảng tĩnh kích thước cố định.

Trong C#, dùng ArrayList collection. Nó đại diện cho tập hợp đối tượng có thứ tự, truy cập bằng index, cho phép cấp phát bộ nhớ động, thêm, tìm kiếm, sắp xếp.

Ví dụ

using System;
using System.Collections;
namespace CollectionApplication {
    class Program {
        static void Main(string[] args) {
            ArrayList al = new ArrayList();
            al.Add(99); al.Add(47); al.Add(64);
            Console.WriteLine("Count: {0}", al.Count);
            Console.Write("List: ");
            foreach (int i in al) Console.Write(i + " ");
            Console.ReadKey();
        }
    }
}

Kết quả

Count: 3 List: 99 47 64