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:
// C# code to remove all // elements from the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection< string > myColl = new Collection< string >(); // Adding elements in Collection myColl myColl.Add( "A" ); myColl.Add( "B" ); myColl.Add( "C" ); myColl.Add( "D" ); myColl.Add( "E" ); // To print the count of elements in Collection Console.WriteLine( "Count : " + myColl.Count); // Displaying the elements in myColl foreach ( string str in myColl) { Console.WriteLine(str); } // Removing all the elements from Collection myColl.Clear(); // To print the count of elements in Collection Console.WriteLine( "Count : " + myColl.Count); // Displaying the elements in myColl foreach ( string str in myColl) { Console.WriteLine(str); } } } |
chevron_right
filter_none
Output:
Count : 5 A B C D E Count : 0
Example 2:
// C# code to remove all // elements from the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection< int > myColl = new Collection< int >(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // To print the count of elements in Collection Console.WriteLine( "Count : " + myColl.Count); // Displaying the elements in myColl foreach ( int i in myColl) { Console.WriteLine(i); } // Removing all the elements from Collection myColl.Clear(); // To print the count of elements in Collection Console.WriteLine( "Count : " + myColl.Count); // Displaying the elements in myColl foreach ( int i in myColl) { Console.WriteLine(i); } } } |
chevron_right
filter_none
Output:
Count : 4 2 3 4 5 Count : 0
Note:
- Count is set to zero, and references to other objects from elements of the collection are also released.
- This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Remove all elements from the Hashtable
- C# | Remove all elements from OrderedDictionary
- C# | Remove all elements from a SortedList
- C# | Remove all elements from a HashSet
- C# | Remove all elements from the ArrayList
- C# | Remove all elements from the SortedSet
- C# | Remove a range of elements from the ArrayList
- C# | Remove all elements in a collection from a HashSet
- C# | Remove elements from a SortedSet that match the predicate
- C# | Remove elements from a HashSet with conditions defined by the predicate
- C# | Remove all elements of a List that match the conditions defined by the predicate
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Remove() Method
- C# | Remove all entries from the ListDictionary
- C# | Remove the entry with specified key from ListDictionary
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.