C# | Removing all the elements from the List Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSharp // 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: CSharp // 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.clear?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Removing all the elements from the List K Kirti_Mangal Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-List CSharp-Collections-Namespace CSharp-Generic-Namespace +2 More Practice Tags : Misc Similar Reads C# | Removing the specified element from the List List.Remove(T) Method is used to remove the first occurrence of a specific object from the List. Properties of List: 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 elem 2 min read C# | Remove all elements from the Collection<T> Collection<T>.Clear method is used to remove all elements from the Collection<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all // elements from the Collection using System; using S 2 min read C# | Remove all elements from a HashSet A HashSet is an unordered collection of the unique elements. It comes under the System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashS 2 min read C# | Remove all elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis 3 min read C# | Remove all elements from a SortedList SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Clear method is used to remove all the elements from a SortedList ob 2 min read Like