Xóa LinkedList bằng phương thức Clear ().
Trước tiên, hãy để chúng tôi thiết lập một Danh sách liên kết.
string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"};
LinkedList<string> list = new LinkedList<string>(employees); Bây giờ, hãy để chúng tôi xóa LinkedList.
list.Clear();
Hãy cho chúng tôi xem mã hoàn chỉnh.
Ví dụ
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"};
LinkedList<string>list = new LinkedList<string>(employees);
foreach (var emp in list) {
Console.WriteLine(emp);
}
// clearing list
list.Clear();
Console.WriteLine("LinkedList after removing the nodes (empty list)...");
foreach (var emp in list) {
Console.WriteLine(emp);
}
}
} Đầu ra
Patrick Robert John Jacob Jamie LinkedList after removing the nodes (empty list)...