Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Generic List<T> ใน C# คืออะไร

List<T> เป็น Generic Collection ใน C# ขนาดเพิ่มได้ไดนามิก (Dynamic) ไม่เหมือน Array ที่ขนาดคงที่

การใช้งาน

  1. ประกาศ List: List<string> myList = new List<string>();
  2. เพิ่มข้อมูล: myList.Add("mammals"); หรือ Initializer:
    List<string> myList = new List<string>() { "mammals", "reptiles", "amphibians" };
  3. นับจำนวน: 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
    }
}