This documentation is archived and is not being maintained.

StringDictionary.CopyTo Method

Copies the string dictionary values to a one-dimensional Array instance at the specified index.

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

Parameters

array
The one-dimensional Array that is the destination of the values copied from the StringDictionary.
index
The index in the array where copying begins.

Exceptions

Exception Type Condition
ArgumentException array is multidimensional.

-or-

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

ArgumentNullException array is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index is less than the lower bound of array.

Remarks

CopyTo copies objects that can be typecast to System.Collections.DictionaryEntry. DictionaryEntry contains both the key and the value.

The elements copied to the Array are sorted in the same order that the enumerator iterates through the StringDictionary.

Example

[Visual Basic, C#, C++] The following code example shows how a StringDictionary can be copied to an array.

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports Microsoft.VisualBasic

Public Class SamplesStringDictionary

   Public Shared Sub Main()

      ' Creates and initializes a new StringDictionary.
      Dim myCol As New StringDictionary()
      myCol.Add("red", "rojo")
      myCol.Add("green", "verde")
      myCol.Add("blue", "azul")

      ' Displays the values in the StringDictionary.
      Console.WriteLine("KEYS" + ControlChars.Tab + "VALUES in the StringDictionary")
      Dim myDE As DictionaryEntry
      For Each myDE In  myCol
         Console.WriteLine("{0}" + ControlChars.Tab + "{1}", myDE.Key, myDE.Value)
      Next myDE
      Console.WriteLine()

      ' Creates an array with DictionaryEntry elements.
      Dim myArr As DictionaryEntry() =  {New DictionaryEntry(), New DictionaryEntry(), New DictionaryEntry()}

      ' Copies the StringDictionary to the array.
      myCol.CopyTo(myArr, 0)

      ' Displays the values in the array.
      Console.WriteLine("KEYS" + ControlChars.Tab + "VALUES in the array")
      Dim i As Integer
      For i = 0 To myArr.Length - 1
         Console.WriteLine("{0}" + ControlChars.Tab + "{1}", myArr(i).Key, myArr(i).Value)
      Next i
      Console.WriteLine()

   End Sub 'Main 

End Class 'SamplesStringDictionary


'This code produces the following output.
'
'KEYS    VALUES in the StringDictionary
'green   verde
'red     rojo
'blue    azul
'
'KEYS    VALUES in the array
'green   verde
'red     rojo
'blue    azul


[C#] 
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringDictionary  {

   public static void Main()  {

      // Creates and initializes a new StringDictionary.
      StringDictionary myCol = new StringDictionary();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );

      // Displays the values in the StringDictionary.
      Console.WriteLine( "KEYS\tVALUES in the StringDictionary" );
      foreach ( DictionaryEntry myDE in myCol )
         Console.WriteLine( "{0}\t{1}", myDE.Key, myDE.Value );
      Console.WriteLine();

      // Creates an array with DictionaryEntry elements.
      DictionaryEntry[] myArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() };

      // Copies the StringDictionary to the array.
      myCol.CopyTo( myArr, 0 );

      // Displays the values in the array.
      Console.WriteLine( "KEYS\tVALUES in the array" );
      for ( int i = 0; i < myArr.Length; i++ )
         Console.WriteLine( "{0}\t{1}", myArr[i].Key, myArr[i].Value );
      Console.WriteLine();

   }

}

/*
This code produces the following output.

KEYS    VALUES in the StringDictionary
green   verde
red     rojo
blue    azul

KEYS    VALUES in the array
green   verde
red     rojo
blue    azul

*/

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

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

void main() {

    // Creates and initializes a new StringDictionary.
    StringDictionary* myCol = new StringDictionary();
    myCol->Add( S"red", S"rojo" );
    myCol->Add( S"green", S"verde" );
    myCol->Add( S"blue", S"azul" );

    // Displays the values in the StringDictionary.
    Console::WriteLine(S"KEYS\tVALUES in the StringDictionary");
    IEnumerator* myEnum = myCol->GetEnumerator();
    while (myEnum->MoveNext()) {
        DictionaryEntry* myDE = __try_cast<DictionaryEntry*>(myEnum->Current);
        Console::WriteLine( S"{0}\t{1}", myDE->Key, myDE->Value );
        Console::WriteLine();

        // Creates an array with DictionaryEntry elements.
        DictionaryEntry myArr[] = new DictionaryEntry[3];

            // Copies the StringDictionary to the array.
            myCol->CopyTo(myArr, 0);

            // Displays the values in the array.
            Console::WriteLine(S"KEYS\tVALUES in the array");
            for (int i = 0; i < myArr->Length; i++)
                Console::WriteLine(S"{0}\t{1}", myArr[i].Key, myArr[i].Value);
            Console::WriteLine();
    }
}

/*
This code produces the following output.

KEYS    VALUES in the StringDictionary
green   verde
red     rojo
blue    azul

KEYS    VALUES in the array
green   verde
red     rojo
blue    azul

*/

[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

StringDictionary Class | StringDictionary Members | System.Collections.Specialized Namespace

Show: