ในการเปิดองค์ประกอบแรกในรายการ ให้ใช้เมธอด RemoveAt() จะลบองค์ประกอบออกจากตำแหน่งที่คุณต้องการลบองค์ประกอบ
ตั้งค่ารายการ
List<string> myList = new List<string>() {
"Operating System",
"Computer Networks",
"Compiler Design"
}; ตอนนี้เปิดองค์ประกอบแรกโดยใช้ RemoveAt(0)
myList.RemoveAt(0);
เรามาดูตัวอย่างฉบับสมบูรณ์กัน
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> myList = new List<string>() {
"Operating System",
"Computer Networks",
"Compiler Design"
};
Console.Write("Initial list...");
foreach (string list in myList) {
Console.WriteLine(list);
}
Console.Write("Removing first element from the list...");
myList.RemoveAt(0);
foreach (string list in myList) {
Console.WriteLine(list);
}
}
} ผลลัพธ์
Initial list... Operating System Computer Networks Compiler Design Removing first element from the list... Computer Networks Compiler Design