Đầu tiên, đặt một danh sách -
List<int> myList = new List<int>(); myList.Add(45); myList.Add(77);
Bây giờ, để xóa danh sách trên, hãy sử dụng Clear () -
myList.Clear();
Đây là mã hoàn chỉnh -
Ví dụ
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> myList = new List<int>();
myList.Add(45);
myList.Add(77);
Console.WriteLine("Elements: "+myList.Count);
myList.Clear();
Console.WriteLine("Elements after using clear: "+myList.Count);
}
} Đầu ra
Phần tửElements: 2 Elements after using clear: 0