Clear a LinkedList using Clear() method.
Let us first set a LinkedList.
string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"}; LinkedList<string> list = new LinkedList<string>(employees);
Now, let us clear the LinkedList.
list.Clear();
Let us see the complete code.
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); } } }
Patrick Robert John Jacob Jamie LinkedList after removing the nodes (empty list)...