
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Removing all nodes from LinkedList in C#
To remove all nodes from LinkedList, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { int [] num = {10, 20, 30, 40, 50}; LinkedList<int> list = new LinkedList<int>(num); Console.WriteLine("LinkedList nodes..."); foreach (var n in list) { Console.WriteLine(n); } list.Clear(); Console.WriteLine("LinkedList is empty now!"); foreach (var n in list) { Console.WriteLine(n); } } }
Output
This will produce the following output −
LinkedList nodes... 10 20 30 40 50 LinkedList is empty now!
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ LinkedList<String> list = new LinkedList<String>(); list.AddLast("A"); list.AddLast("B"); list.AddLast("C"); list.AddLast("D"); list.AddLast("E"); list.AddLast("F"); list.AddLast("G"); list.AddLast("H"); list.AddLast("I"); list.AddLast("J"); Console.WriteLine("Count of nodes = " + list.Count); list.Clear(); Console.WriteLine("Count of nodes (updated) = " + list.Count); } }
Output
This will produce the following output −
Count of nodes = 10 Count of nodes (updated) = 0
- Related Articles
- Removing the specified node from the LinkedList in C#?
- Removing first occurrence of specified value from LinkedList in C#
- Removing all entries from HybridDictionary in C#
- Removing all entries from the StringDictionary in C#
- Get the number of nodes contained in LinkedList in C#
- Removing all the empty indices from array in JavaScript
- Removing all the elements from the List in C#
- Removing the node at the start of the LinkedList in C#
- Removing all spaces from a string using JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- How to remove all child nodes from a parent in jQuery?
- Delete all Prime Nodes from a Doubly Linked List in C++
- Delete all Prime Nodes from a Singly Linked List in C++
- Print all nodes at distance k from a given node in C++
- Delete all Non-Prime Nodes from a Singly Linked List in C++

Advertisements