StringCollection.CopyTo(String[], Int32) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Kopieert de volledige StringCollection waarden naar een eendimensionale matrix met tekenreeksen, te beginnen bij de opgegeven index van de doelmatrix.
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)
Parameters
- array
- String[]
De eendimensionale matrix van tekenreeksen die het doel is van de elementen die zijn gekopieerd uit StringCollection. De Array indexering op basis van nul moet zijn.
- index
- Int32
De op nul gebaseerde index array waarin kopiëren begint.
Uitzonderingen
array is null.
index is kleiner dan nul.
array is multidimensionaal.
– of –
Het aantal elementen in de bron StringCollection is groter dan de beschikbare ruimte van index tot het einde van de bestemming array.
Het type bron StringCollection kan niet automatisch worden gecast naar het type van de bestemming array.
Voorbeelden
In het volgende codevoorbeeld wordt een StringCollection naar een matrix gekopieerd.
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
'
Opmerkingen
De opgegeven matrix moet van een compatibel type zijn.
De elementen worden gekopieerd naar dezelfde Array volgorde waarin de opsomming van de StringCollection iterator door de StringCollection.
Deze methode is een O(n)-bewerking, waarbij n .Count