Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

Hashtable.CopyTo Method

Copies the Hashtable elements to a one-dimensional Array instance at the specified index.

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

public:
virtual void CopyTo (
	Array^ array, 
	int arrayIndex
)
public void CopyTo (
	Array array, 
	int arrayIndex
)
public function CopyTo (
	array : Array, 
	arrayIndex : int
)
Not applicable.

Parameters

array

The one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. The Array must have zero-based indexing.

arrayIndex

The zero-based index in array at which copying begins.

Exception typeCondition

ArgumentNullException

array is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

arrayIndex is less than zero.

ArgumentException

array is multidimensional.

-or-

arrayIndex is equal to or greater than the length of array.

-or-

The number of elements in the source Hashtable is greater than the available space from arrayIndex to the end of the destination array.

InvalidCastException

The type of the source Hashtable cannot be cast automatically to the type of the destination array.

The elements are copied to the Array in the same order in which the enumerator iterates through the Hashtable.

To copy only the keys in the Hashtable, use Hashtable.Keys.CopyTo.

To copy only the values in the Hashtable, use Hashtable.Values.CopyTo.

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

The following example shows how to copy the list of keys or the list of values in a Hashtable into a one-dimensional Array.

using namespace System;
using namespace System::Collections;
void PrintValues( array<String^>^myArr, char mySeparator );
int main()
{
   
   // Creates and initializes the source Hashtable.
   Hashtable^ mySourceHT = gcnew Hashtable;
   mySourceHT->Add( "A", "valueA" );
   mySourceHT->Add( "B", "valueB" );
   
   // Creates and initializes the one-dimensional target Array.
   array<String^>^myTargetArray = gcnew array<String^>(15);
   myTargetArray[ 0 ] = "The";
   myTargetArray[ 1 ] = "quick";
   myTargetArray[ 2 ] = "brown";
   myTargetArray[ 3 ] = "fox";
   myTargetArray[ 4 ] = "jumped";
   myTargetArray[ 5 ] = "over";
   myTargetArray[ 6 ] = "the";
   myTargetArray[ 7 ] = "lazy";
   myTargetArray[ 8 ] = "dog";
   
   // Displays the values of the target Array.
   Console::WriteLine( "The target Array contains the following before:" );
   PrintValues( myTargetArray, ' ' );
   
   // Copies the keys in the source Hashtable to the target Hashtable, starting at index 6.
   Console::WriteLine( "After copying the keys, starting at index 6:" );
   mySourceHT->Keys->CopyTo( myTargetArray, 6 );
   
   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );
   
   // Copies the values in the source Hashtable to the target Hashtable, starting at index 6.
   Console::WriteLine( "After copying the values, starting at index 6:" );
   mySourceHT->Values->CopyTo( myTargetArray, 6 );
   
   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );
}

void PrintValues( array<String^>^myArr, char mySeparator )
{
   for ( int i = 0; i < myArr->Length; i++ )
      Console::Write( "{0}{1}", mySeparator, myArr[ i ] );
   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The target Array contains the following before:
  The quick brown fox jumped over the lazy dog
 After copying the keys, starting at index 6:
  The quick brown fox jumped over B A dog
 After copying the values, starting at index 6:
  The quick brown fox jumped over valueB valueA dog

 */

import System.*;
import System.Collections.*;

public class SamplesHashtable
{
    public static void main(String[] args)
    {
        // Creates and initializes the source Hashtable.
        Hashtable mySourceHT = new Hashtable();

        mySourceHT.Add("A", "valueA");
        mySourceHT.Add("B", "valueB");

        // Creates and initializes the one-dimensional target Array.
        String myTargetArray[] = new String[15];

        myTargetArray.set_Item(0, "The");
        myTargetArray.set_Item(1, "quick");
        myTargetArray.set_Item(2, "brown");
        myTargetArray.set_Item(3, "fox");
        myTargetArray.set_Item(4, "jumped");
        myTargetArray.set_Item(5, "over");
        myTargetArray.set_Item(6, "the");
        myTargetArray.set_Item(7, "lazy");
        myTargetArray.set_Item(8, "dog");

        // Displays the values of the target Array.
        Console.WriteLine("The target Array contains the following before:");
        PrintValues(myTargetArray, ' ');

        // Copies the keys in the source Hashtable to the target Hashtable, 
        // starting at index 6.
        Console.WriteLine("After copying the keys, starting at index 6:");
        mySourceHT.get_Keys().CopyTo(myTargetArray, 6);

        // Displays the values of the target Array.
        PrintValues(myTargetArray, ' ');

        // Copies the values in the source Hashtable to the target Hashtable, 
        // starting at index 6.
        Console.WriteLine("After copying the values, starting at index 6:");
        mySourceHT.get_Values().CopyTo(myTargetArray, 6);

        // Displays the values of the target Array.
        PrintValues(myTargetArray, ' ');
    } //main

    public static void PrintValues(String myArr[], char mySeparator)
    {
        for (int i = 0; i < myArr.length; i++) {
            Console.Write("{0}{1}", System.Convert.ToString(mySeparator), 
                myArr.get_Item(i));
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesHashtable
/* 
 This code produces the following output.
 
 The target Array contains the following before:
  The quick brown fox jumped over the lazy dog
 After copying the keys, starting at index 6:
  The quick brown fox jumped over B A dog
 After copying the values, starting at index 6:
  The quick brown fox jumped over valueB valueA dog
 */

import System
import System.Collections

// Creates and initializes the source Hashtable.
var mySourceHT : Hashtable = new Hashtable()
mySourceHT.Add("A", "valueA")
mySourceHT.Add("B", "valueB")

// Creates and initializes the one-dimensional target Array.
var myTargetArray : System.Array = System.Array.CreateInstance(System.String, 15)
myTargetArray.SetValue("The", 0)
myTargetArray.SetValue("quick", 1)
myTargetArray.SetValue("brown", 2)
myTargetArray.SetValue("fox", 3)
myTargetArray.SetValue("jumped", 4)
myTargetArray.SetValue("over", 5)
myTargetArray.SetValue("the", 6)
myTargetArray.SetValue("lazy", 7)
myTargetArray.SetValue("dog", 8)

// Displays the values of the target Array.
Console.WriteLine("The target Array contains the following before:")
PrintValues(myTargetArray, " ")

// Copies the keys in the source Hashtable to the target Hashtable,
// starting at index 6.
Console.WriteLine("After copying the keys, starting at index 6:")
mySourceHT.Keys.CopyTo(myTargetArray, 6)

// Displays the values of the target Array.
PrintValues(myTargetArray, " ")

// Copies the values in the source Hashtable to the target Hashtable,
// starting at index 6.
Console.WriteLine("After copying the values, starting at index 6:")
mySourceHT.Values.CopyTo(myTargetArray, 6)

// Displays the values of the target Array.
PrintValues(myTargetArray, " ")

function PrintValues(myArr : System.Array, mySeparator : Char){
    var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator()
    var i : int = 0
    var cols : int = myArr.GetLength(myArr.Rank - 1)
    while(myEnumerator.MoveNext()){
        if(i < cols)
            i++
        else{
            Console.WriteLine()
            i = 1
        }
        Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
    }
    Console.WriteLine()
}

// This code produces the following output.
// 
// The target Array contains the following before:
//  The quick brown fox jumped over the lazy dog
// After copying the keys, starting at index 6:
//  The quick brown fox jumped over A B dog
// After copying the values, starting at index 6:
//  The quick brown fox jumped over valueA valueB dog 

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, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.