C# | Remove all the strings from the StringCollection
StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace.
StringCollection.Clear method is used to remove all the strings from the StringCollection.
Syntax:
public void Clear ();
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.
Example:
// C# code to insert a string into // the StringCollection at the // specified index using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // creating a string array named myArr String[] myArr = new String[] {"Hello", "Geeks", "for", "GeeksforGeeks"}; // Copying the elements of a string // array to the end of the StringCollection. myCol.AddRange(myArr); Console.WriteLine("Initially elements in StringCollection are: "); // Displaying elements in StringCollection // named myCol foreach(Object obj in myCol) Console.WriteLine(obj); // Removing all the elements from StringCollection myCol.Clear(); Console.WriteLine("After Removing: "); // Displaying elements in StringCollection // named myCol foreach(Object obj in myCol) Console.WriteLine(obj); } } |
chevron_right
filter_none
Output:
Initially elements in StringCollection are: Hello Geeks for GeeksforGeeks After Removing:
Reference:
Recommended Posts:
- C# | Remove the first occurrence from the StringCollection
- C# | Remove from the specified index of the StringCollection
- C# | Get the number of strings in StringCollection
- C# | StringCollection Class
- C# | Get or Set at specified index in StringCollection
- C# | Add a string to the end of the StringCollection
- How to create a StringCollection in C#
- C# | Index of first occurrence in StringCollection
- C# | Check if the specified string is in the StringCollection
- C# | Get an enumerator that iterates through StringCollection
- C# | Insert at the specified index in StringCollection
- C# | Check if the StringCollection is read-only
- How to get Synchronize access to the StringCollection in C#
- C# | Check if two StringCollection objects are equal
- C# | Gets or sets the element at the specified index in StringCollection
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.



