StringCollection类是.NET Framework类库的新添加,它表示字符串的集合。 StringCollection类在System.Collections.Specialized命名空间中定义。
特征:
- StringCollection类接受null作为有效值,并允许重复的元素。
- 字符串比较区分大小写。
- 可以使用整数索引访问此集合中的元素。
- 此集合中的索引从零开始。
建设者
Constructor | Description |
---|---|
StringCollection() | Initializes a new instance of the StringCollection class. |
例子:
// C# code to create a StringCollection
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();
// Adding elements in StringCollection
myCol.Add("A");
myCol.Add("B");
myCol.Add("C");
myCol.Add("D");
myCol.Add("E");
// Displaying objects in myCol
foreach(Object obj in myCol)
{
Console.WriteLine(obj);
}
}
}
输出:
A
B
C
D
E
特性
Property | Description |
---|---|
Count | Gets the number of strings contained in the StringCollection. |
IsReadOnly | Gets a value indicating whether the StringCollection is read-only. |
IsSynchronized | Gets a value indicating whether access to the StringCollection is synchronized (thread safe). |
Item[Int32] | Gets or sets the element at the specified index. |
SyncRoot | Gets an object that can be used to synchronize access to the StringCollection. |
例子:
// C# code to illustrate the StringCollection
// Class Properties
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[] { "A", "B", "C", "D", "E" };
// Copying the elements of a string
// array to the end of the StringCollection.
myCol.AddRange(myArr);
// --------- Using IsReadOnly Property ---------
// checking if StringCollection is
// read-only
Console.WriteLine(myCol.IsReadOnly);
// --------- Using Count Property ---------
// To get number of Strings contained
// in the StringCollection
Console.WriteLine("Number of strings in myCol are : "
+ myCol.Count);
}
}
输出:
False
Number of strings in myCol are : 5
方法
Method | Description |
---|---|
Add(String) | Adds a string to the end of the StringCollection. |
AddRange(String[]) | Copies the elements of a string array to the end of the StringCollection. |
Clear() | Removes all the strings from the StringCollection. |
Contains(String) | Determines whether the specified string is in the StringCollection. |
CopyTo(String[], Int32) | Copies the entire StringCollection values to a one-dimensional array of strings, starting at the specified index of the target array. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns a StringEnumerator that iterates through the StringCollection. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
IndexOf(String) | Searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection. |
Insert(Int32, String) | Inserts a string into the StringCollection at the specified index. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Remove(String) | Removes the first occurrence of a specific string from the StringCollection. |
RemoveAt(Int32) | Removes the string at the specified index of the StringCollection. |
ToString() | Returns a string that represents the current object. |
例子:
// C# code to copy StringCollection to array,
// starting at the specified index of
// the target array
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 myArr1
String[] myArr1 = new String[] { "A", "B", "C", "D", "E" };
// Copying the elements of a string
// array to the end of the StringCollection.
myCol.AddRange(myArr1);
// creating a String array named myArr2
String[] myArr2 = new String[myCol.Count];
// Copying StringCollection to array myArr2
// starting from index 0
myCol.CopyTo(myArr2, 0);
// Displaying elements in array myArr2
for (int i = 0; i < myArr2.Length; i++) {
Console.WriteLine(myArr2[i]);
}
}
}
输出:
A
B
C
D
E
例子:
// 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?view=netframework-4.7.2