List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. List.Clear Method is used remove the all the elements from the List.
Properties:
- It is different from the arrays. A list can be resized dynamically but arrays cannot.
- List class can accept null as a valid value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity, then the capacity of the List increased automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
Syntax:
public void Clear ();
Below programs illustrate how to remove all the elements from the List:
Example 1:
// C# program to remove all the // elements from a List using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating a List of integers List<int> list1 = new List<int>(); // Inserting the elements into the List list1.Add(1); list1.Add(4); list1.Add(3); list1.Add(1); list1.Add(2); // Displaying the count of elements // contained in the List before // removing all the elements Console.Write("Number of elements in the List Before Removing: "); // using Count property Console.WriteLine(list1.Count); // Removing all elements from list list1.Clear(); // Displaying the count of elements // contained in the List after // removing all the elements Console.Write("Number of elements in the List After Removing: "); // using Count property Console.WriteLine(list1.Count); } } |
Output:
Number of elements in the List Before Removing: 5 Number of elements in the List After Removing: 0
Example 2:
// C# program to remove all the // elements from a List using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating a List of strings List<string> list1 = new List<string>(); // Inserting the elements into the List list1.Add("Welcome"); list1.Add("To"); list1.Add("Geeks"); list1.Add("for"); list1.Add("Geeks"); list1.Add("Geeks"); list1.Add("Geeks"); // Displaying the count of elements // contained in the List before // removing all the elements Console.Write("Number of elements in the List Before Removing: "); // using Count property Console.WriteLine(list1.Count); // Removing all elements from list list1.Clear(); // Displaying the count of elements // contained in the List after // removing all the elements Console.Write("Number of elements in the List After Removing: "); // using Count property Console.WriteLine(list1.Count); } } |
Output:
Number of elements in the List Before Removing: 7 Number of elements in the List After Removing: 0
Reference:
Recommended Posts:
- C# | Removing a range of elements from the List
- C# | Removing the specified element from the List
- C# | Removing all entries from the StringDictionary
- C# | Removing all entries from HybridDictionary
- C# | Removing all nodes from LinkedList<T>
- C# | Removing the specified key entry from HybridDictionary
- C# | Removing first occurrence of object from Collection<T>
- C# | Removing the node at the start of the LinkedList<T>
- C# | Removing the node at the end of LinkedList<T>
- C# | Removing the specified node from the LinkedList<T>
- C# | Removing first occurrence of specified value from LinkedList<T>
- Removing the object from the top of the Stack in C#
- C# | How to get all elements of a List that match the conditions specified by the predicate
- C# | Remove all elements of a List that match the conditions defined by the predicate
- How to sort a list in C# | List.Sort() Method Set -1
- How to sort a list in C# | List.Sort() Method Set -2
- C# | Count the total number of elements in the List
- C# | Adding the elements of the specified collection to the end of the List
- C# | How to insert the elements of a collection into the List at the specified index
- C# | How to check whether a List contains the elements that match the specified conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

