Để xóa một mục khỏi danh sách trong C # bằng chỉ mục, hãy sử dụng phương thức RemoveAt ().
Đầu tiên, đặt danh sách -
List<string> list1 = new List<string>() { "Hanks", "Lawrence", "Beckham", "Cooper", };
Bây giờ, hãy xóa phần tử ở vị trí thứ 2, tức là chỉ mục 1
list1.RemoveAt(1);
Hãy để chúng tôi xem ví dụ hoàn chỉnh -
Ví dụ
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<string> list1 = new List<string>() { "Hanks", "Lawrence", "Beckham", "Cooper", }; Console.Write("Initial list..."); foreach (string list in list1) { Console.WriteLine(list); } Console.Write("Removing element from the list..."); list1.RemoveAt(1); foreach (string list in list1) { Console.WriteLine(list); } } }
Đầu ra
Initial list... Hanks Lawrence Beckham Cooper Removing element from the list... Hanks Beckham Cooper