StringCollection类是.NET Framework类库的新添加,它表示字符串的集合。 StringCollection类在System.Collections.Specialized命名空间中定义。
StringCollection.Count属性用于获取StringCollection中包含的字符串数。
句法:
public int Count { get; }
返回值:返回StringCollection中包含的字符串数。
注意:检索此属性的值是O(1)操作。
下面的程序说明StringCollection.Count属性的用法:
范例1:
// C# code to get the number of strings
// contained in the 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();
// creating a string array named myArr
String[] myArr = new String[] { "First", "Second", "Third",
"Fourth", "Fifth" };
// Copying the elements of a string
// array to the end of the StringCollection.
myCol.AddRange(myArr);
// To get number of Strings contained
// in the StringCollection
Console.WriteLine("Number of strings in myCol are : " + myCol.Count);
}
}
输出:
Number of strings in myCol are : 5
范例2:
// C# code to get the number of strings
// contained in the 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();
// creating a string array named myArr
String[] myArr = new String[] {};
// Copying the elements of a string
// array to the end of the StringCollection.
myCol.AddRange(myArr);
// To get number of Strings contained
// in the StringCollection
Console.WriteLine("Number of strings in myCol are : "
+ myCol.Count);
}
}
输出:
Number of strings in myCol are : 0
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringcollection.count?view=netframework-4.7.2