Export (0) Print
Expand All

StringDictionary.CopyTo Method

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

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

Public Overridable Sub CopyTo ( _
	array As Array, _
	index As Integer _


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.


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 Nothing.


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.

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

      ' 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

   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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft