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 virtual void CopyTo (
	Array array,
	int index
)
public void CopyTo (
	Array array, 
	int index
)
public function CopyTo (
	array : Array, 
	index : int
)
Not applicable.

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.

Exception typeCondition

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.

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

*/

import System.* ;
import System.Collections.IEnumerator;
import System.Collections.DictionaryEntry;
import System.Collections.Specialized.* ;

public class SamplesStringDictionary
{   
    public static void main(String[] args)
    {                
        // 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");
        IEnumerator objEnum = myCol.GetEnumerator();
        while (objEnum.MoveNext()) { 
            DictionaryEntry myDE = (DictionaryEntry)objEnum.get_Current();
            Console.WriteLine("{0}\t{1}", myDE.get_Key(), myDE.get_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].get_Key(),
                myArr[i].get_Value());
        } 
        Console.WriteLine();
    } //main 
} //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

*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft