Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Kopiert alle StringCollection-Werte beginnend am angegebenen Index des Zielarrays in ein eindimensionales Zeichenfolgenarray.
Namespace: System.Collections.Specialized
Assembly: System (in system.dll)
Syntax
'Declaration
Public Sub CopyTo ( _
array As String(), _
index As Integer _
)
'Usage
Dim instance As StringCollection
Dim array As String()
Dim index As Integer
instance.CopyTo(array, index)
public void CopyTo (
string[] array,
int index
)
public:
void CopyTo (
array<String^>^ array,
int index
)
public void CopyTo (
String[] array,
int index
)
public function CopyTo (
array : String[],
index : int
)
Parameter
- array
Das eindimensionale Zeichenfolgenarray, das das Ziel der aus StringCollection kopierten Elemente ist. Für Array muss eine nullbasierte Indizierung verwendet werden.
- index
Der nullbasierte Index in array, ab dem kopiert wird.
Ausnahmen
| Ausnahmetyp | Bedingung |
|---|---|
array ist NULL (Nothing in Visual Basic). |
|
index ist kleiner als 0 (null). |
|
Das array ist mehrdimensional. - oder - index ist größer oder gleich der Länge von array. - oder - Die Anzahl der aus der Quell-StringCollection zu kopierenden Elemente ist größer als der verfügbare Platz von index bis zum Ende des Ziel-array. |
|
Der Typ der Quell-StringCollection kann nicht automatisch in den Typ des Ziel-array umgewandelt werden. |
Hinweise
Der Typ des angegebenen Arrays muss kompatibel sein.
Die Elemente werden in derselben Reihenfolge nach Array kopiert, in der der Enumerator der StringCollection die StringCollection durchläuft.
Diese Methode ist eine O(n)-Operation, wobei n gleich Count ist.
Beispiel
Im folgenden Codebeispiel wird eine StringCollection in ein Array kopiert.
Imports System
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 'Main
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 'PrintValues
End Class 'SamplesStringCollection
'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
*/
#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
*/
import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;
public class SamplesStringCollection
{
public static void main(String[] args)
{
// 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.get_Count()];
myCol.CopyTo(myArr2, 0);
Console.WriteLine("The new array contains:");
for (int i=0; i < myArr2.length; i++) {
Console.WriteLine(" [{0}] {1}",System.Convert.ToString(i),
myArr2.get_Item(i));
}
Console.WriteLine();
} //main
public static void PrintValues(IEnumerable myCol)
{
Object obj = null;
IEnumerator objEnum = myCol.GetEnumerator();
while (objEnum.MoveNext()) {
obj = objEnum.get_Current();
Console.WriteLine(" {0}", obj);
}
Console.WriteLine();
} //PrintValues
} //SamplesStringCollection
/*
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
*/
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
StringCollection-Klasse
StringCollection-Member
System.Collections.Specialized-Namespace
Array-Klasse
GetEnumerator