Export (0) Print
Expand All

Hashtable.CopyTo Method

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

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

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.

Implements

ICollection.CopyTo

Exceptions

Exception Type Condition
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.

Remarks

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.

Example

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

[Visual Basic] 
Imports System
Imports System.Collections

Public Class SamplesHashtable    
    
    Public Shared Sub Main()
        
        ' Creates and initializes the source Hashtable.
        Dim mySourceHT As New Hashtable()
        mySourceHT.Add("A", "valueA")
        mySourceHT.Add("B", "valueB")
        
        ' Creates and initializes the one-dimensional target Array.
        Dim myTargetArray As Array = Array.CreateInstance(GetType(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, " "c)
        
        ' 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, " "c)
        
        ' 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, " "c)
    End Sub
    
    
    Public Shared Sub PrintValues(myArr As Array, mySeparator As Char)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' 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 

[C#] 
using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // 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.
      Array myTargetArray=Array.CreateInstance( typeof(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, ' ' );
   }

   public static void PrintValues( Array myArr, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = 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
*/ 

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

using namespace System;
using namespace System::Collections;

void PrintValues( Array* myArr, char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myArr->GetEnumerator();
   int i = 0;
   int cols = myArr->GetLength( myArr->Rank - 1 );
   while ( myEnumerator->MoveNext() )  {
      if ( i < cols )  {
         i++;
      } else  {
         Console::WriteLine();
         i = 1;
      }
      Console::Write( "{0}{1}", __box(mySeparator), myEnumerator->Current );
   }
   Console::WriteLine();
}

int main()  {
   // Creates and initializes the source Hashtable.
   Hashtable* mySourceHT = new Hashtable();
   mySourceHT->Add( S"A", S"valueA" );
   mySourceHT->Add( S"B", S"valueB" );

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

   // Displays the values of the target Array.
   Console::WriteLine( S"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( S"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( S"After copying the values, starting at index 6:" );
   mySourceHT->Values->CopyTo( myTargetArray, 6 );

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

 /*
 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
 */

[JScript] 
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 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Hashtable Class | Hashtable Members | System.Collections Namespace | Array | DictionaryEntry | GetEnumerator

Show:
© 2015 Microsoft