StringCollection.Count 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 StringCollection 中包含的字符串的数目。
public:
property int Count { int get(); };
public int Count { get; }
member this.Count : int
Public ReadOnly Property Count As Integer
属性值
StringCollection 中包含的字符串的数目。
实现
示例
下面的代码示例将 复制到 StringCollection 数组。
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues( IEnumerable^ myCol );
int main()
{
// Creates and initializes a new StringCollection.
StringCollection^ myCol = gcnew StringCollection;
array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
myCol->AddRange( myArr );
Console::WriteLine( "Initial contents of the StringCollection:" );
PrintValues( myCol );
// Copies the collection to a new array starting at index 0.
array<String^>^myArr2 = gcnew array<String^>(myCol->Count);
myCol->CopyTo( myArr2, 0 );
Console::WriteLine( "The new array contains:" );
for ( int i = 0; i < myArr2->Length; i++ )
{
Console::WriteLine( " [{0}] {1}", i, myArr2[ i ] );
}
Console::WriteLine();
}
void PrintValues( IEnumerable^ myCol )
{
IEnumerator^ myEnum = myCol->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( " {0}", obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
Initial contents of the StringCollection:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
The new array contains:
[0] RED
[1] orange
[2] yellow
[3] RED
[4] green
[5] blue
[6] RED
[7] indigo
[8] violet
[9] RED
*/
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringCollection {
public static void Main() {
// Creates and initializes a new StringCollection.
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
myCol.AddRange( myArr );
Console.WriteLine( "Initial contents of the StringCollection:" );
PrintValues( myCol );
// Copies the collection to a new array starting at index 0.
String[] myArr2 = new String[myCol.Count];
myCol.CopyTo( myArr2, 0 );
Console.WriteLine( "The new array contains:" );
for ( int i = 0; i < myArr2.Length; i++ ) {
Console.WriteLine( " [{0}] {1}", i, myArr2[i] );
}
Console.WriteLine();
}
public static void PrintValues( IEnumerable myCol ) {
foreach ( Object obj in myCol )
Console.WriteLine( " {0}", obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
Initial contents of the StringCollection:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
The new array contains:
[0] RED
[1] orange
[2] yellow
[3] RED
[4] green
[5] blue
[6] RED
[7] indigo
[8] violet
[9] RED
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringCollection
Public Shared Sub Main()
' Creates and initializes a new StringCollection.
Dim myCol As New StringCollection()
Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
myCol.AddRange(myArr)
Console.WriteLine("Initial contents of the StringCollection:")
PrintValues(myCol)
' Copies the collection to a new array starting at index 0.
Dim myArr2(myCol.Count) As [String]
myCol.CopyTo(myArr2, 0)
Console.WriteLine("The new array contains:")
Dim i As Integer
For i = 0 To myArr2.Length - 1
Console.WriteLine(" [{0}] {1}", i, myArr2(i))
Next i
Console.WriteLine()
End Sub
Public Shared Sub PrintValues(myCol As IEnumerable)
Dim obj As [Object]
For Each obj In myCol
Console.WriteLine(" {0}", obj)
Next obj
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Initial contents of the StringCollection:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'The new array contains:
' [0] RED
' [1] orange
' [2] yellow
' [3] RED
' [4] green
' [5] blue
' [6] RED
' [7] indigo
' [8] violet
' [9] RED
'
注解
检索此属性的值的运算复杂度为 O(1)。