StringCollection.CopyTo(String[], Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
複製整個 StringCollection 值至字串的一維陣列,由目標陣列的指定索引開始。
public:
void CopyTo(cli::array <System::String ^> ^ array, int index);
public void CopyTo (string[] array, int index);
member this.CopyTo : string[] * int -> unit
Public Sub CopyTo (array As String(), index As Integer)
參數
- array
- String[]
字串的一維陣列,是從 StringCollection 複製的元素之目的端。 Array 必須有以零為起始的索引。
- index
- Int32
array
中以零起始的索引,即開始複製的位置。
例外狀況
array
為 null
。
index
小於零。
來源 StringCollection 的類型無法自動轉換成目的 array
的類型。
範例
下列程式代碼範例會將 複製到陣列程式代碼範例會將 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
'
備註
指定的陣列必須是相容的類型。
元素會以逐一查看 StringCollection的列舉值StringCollection順序複製到 Array 。
這個方法是 O (n
) 作業,其中 n
是 Count。