ในการประกาศและ initialize List ใน C#:
- ประกาศ List:
List<string> myList = new List<string>();
- เพิ่มสมาชิกพร้อม initialize:
List<string> myList = new List<string>() { "one", "two", "three", "four", "five", "six" };
ตัวอย่างเต็ม
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> myList = new List<string>() {
"one", "two", "three", "four", "five", "six"
};
Console.WriteLine(myList.Count);
}
}
ผลลัพธ์
6