清除C#中的链接列表

使用Clear()方法清除LinkedList 。

让我们首先设置一个LinkedList。

string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"};
LinkedList<string> list = new LinkedList<string>(employees);

现在,让我们清除LinkedList。

list.Clear();

让我们看完整的代码。

示例

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);
      }
      //清除列表
      list.Clear();
      Console.WriteLine("LinkedList after removing the nodes (empty list)...");
      foreach (var emp in list) {
         Console.WriteLine(emp);
      }
   }
}

输出结果

Patrick
Robert
John
Jacob
Jamie
LinkedList after removing the nodes (empty list)...