Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

StringCollection.CopyTo Method

Copies the entire StringCollection values to a one-dimensional array of strings, starting at the specified index of the target array.

[Visual Basic]
Public Sub CopyTo( _
   ByVal array() As String, _
   ByVal index As Integer _
)
[C#]
public void CopyTo(
 string[] array,
 int index
);
[C++]
public: void CopyTo(
 String* array __gc[],
 int index
);
[JScript]
public function CopyTo(
   array : String[],
 index : int
);

Parameters

array
The one-dimensional array of strings that is the destination of the elements copied from StringCollection. The Array must have zero-based indexing.
index
The zero-based index in array at which copying begins.

Exceptions

Exception Type Condition
ArgumentNullException array is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index is less than zero.
ArgumentException array is multidimensional.

-or-

index is equal to or greater than the length of array.

-or-

The number of elements in the source StringCollection is greater than the available space from index to the end of the destination array.

InvalidCastException The type of the source StringCollection cannot be cast automatically to the type of the destination array.

Remarks

The specified array must be of a compatible type.

The elements are copied to the Array in the same order in which the enumerator of the StringCollection iterates through the StringCollection.

Example

[Visual Basic, C#, C++] The following code example copies a StringCollection to an array.

[Visual Basic] 
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 myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()

      While myEnumerator.MoveNext()
         Console.WriteLine("   {0}", myEnumerator.Current)
      End While
      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
'


[C#] 
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 )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      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

*/

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintValues(IEnumerable* myCol) {
   System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine(S"   {0}", myEnumerator->Current);
   Console::WriteLine();
}
int main() {

   // Creates and initializes a new StringCollection.
   StringCollection* myCol = new StringCollection();
   String* myArr[] = { S"RED", S"orange", S"yellow", S"RED", S"green", S"blue", S"RED", S"indigo", S"violet", S"RED" };
   myCol->AddRange(myArr);

   Console::WriteLine(S"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(S"The new array contains:");
   for (int i = 0; i < myArr2->Length; i++) {
      Console::WriteLine(S"   [{0}] {1}", __box(i), myArr2->Item[i]);
   }
   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

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

StringCollection Class | StringCollection Members | System.Collections.Specialized Namespace | Array | GetEnumerator

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft