StringCollection.CopyTo Method

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

Namespace: System.Collections.Specialized
Assembly: System (in system.dll)

public void CopyTo (
	string[] array,
	int index
)
public void CopyTo (
	String[] array, 
	int index
)
public function CopyTo (
	array : String[], 
	index : int
)
Not applicable.

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.

Exception typeCondition

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.

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.

This method is an O(n) operation, where n is Count.

The following code example copies a StringCollection to an array.

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

*/

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

*/

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: