List<T> เป็น Generic Collection ใน C# ขนาดเพิ่มได้ไดนามิก (Dynamic) ไม่เหมือน Array ที่ขนาดคงที่
การใช้งาน
- ประกาศ List:
List<string> myList = new List<string>(); - เพิ่มข้อมูล:
myList.Add("mammals");หรือ Initializer:List<string> myList = new List<string>() { "mammals", "reptiles", "amphibians" }; - นับจำนวน: Property
Count
ตัวอย่าง
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> myList = new List<string>() { "mammals", "reptiles", "amphibians" };
Console.WriteLine(myList.Count); // Output: 3
}
}