C# | Removing all the elements from the List
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.
- 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.
public void Clear ();
Below programs illustrate how to remove all the elements from the List:
Number of elements in the List Before Removing: 5 Number of elements in the List After Removing: 0
Number of elements in the List Before Removing: 7 Number of elements in the List After Removing: 0