StringCollection类是.NET Framework类库的新添加,它表示字符串的集合。 StringCollection类在System.Collections.Specialized命名空间中定义。
StringCollection.Clear方法用于从StringCollection中删除所有字符串。
句法:
public void Clear ();
笔记:
- Count设置为零,并且还会释放对集合元素中其他对象的引用。
- 此方法是O(n)运算,其中n是Count。
例子:
// 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);
}
}
输出:
Initially elements in StringCollection are:
Hello
Geeks
for
GeeksforGeeks
After Removing:
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringcollection.clear?view=netframework-4.7.2