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:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "Hello" , "Geeks" ,
"for" , "GeeksforGeeks" };
myCol.AddRange(myArr);
Console.WriteLine( "Initially elements in StringCollection are: " );
foreach (Object obj in myCol)
Console.WriteLine(obj);
myCol.Clear();
Console.WriteLine( "After Removing: " );
foreach (Object obj in myCol)
Console.WriteLine(obj);
}
}
|
Output:
Initially elements in StringCollection are:
Hello
Geeks
for
GeeksforGeeks
After Removing:
Reference: