StringDictionary::CopyTo Method (Array^, Int32)


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

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

virtual void CopyTo(
	Array^ array,
	int index


Type: System::Array^

The one-dimensional Array that is the destination of the values copied from the StringDictionary.

Type: System::Int32

The index in the array where copying begins.

Exception Condition

array is multidimensional.


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


array is null.


index is less than the lower bound of array.

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.

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

The following code example shows how a StringDictionary can be copied to an array.

#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 = gcnew 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" );
   IEnumerator^ myEnum = myCol->GetEnumerator();
   while ( myEnum->MoveNext() )
      DictionaryEntry^ myDE = safe_cast<DictionaryEntry^>(myEnum->Current);
      Console::WriteLine( "{0}\t{1}", myDE->Key, myDE->Value );

      // Creates an array with DictionaryEntry elements.
      array<DictionaryEntry>^myArr = gcnew array<DictionaryEntry>(3);

      // 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 );

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


Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top