Leer en inglés

Compartir a través de


Hashtable.CopyTo(Array, Int32) Método

Definición

Copia los elementos de Hashtable a una instancia unidimensional de Array en el índice especificado.

C#
public virtual void CopyTo (Array array, int arrayIndex);

Parámetros

array
Array

Array unidimensional que constituye el destino de los objetos DictionaryEntry copiados de Hashtable. Array debe tener una indización de base cero.

arrayIndex
Int32

Índice de base cero en la array donde comienza la copia.

Implementaciones

Excepciones

array es null.

arrayIndex es menor que cero.

array es multidimensional.

O bien

El número de elementos de la Hashtable de origen es mayor que el espacio disponible desde arrayIndex hasta el final de la array de destino.

El tipo de la Hashtable de origen no puede convertirse automáticamente al tipo de la array de destino.

Ejemplos

En el ejemplo siguiente se muestra cómo copiar la lista de claves o la lista de valores de en en Hashtable una unidimensional Array.

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

   public static void Main()
   {
      // Creates and initializes the source Hashtable.
      var mySourceHT = new Hashtable();
      mySourceHT.Add("A", "valueA");
      mySourceHT.Add("B", "valueB");

      // Creates and initializes the one-dimensional target Array.
      var myTargetArray = new String[15];
      myTargetArray[0] = "The";
      myTargetArray[1] = "quick";
      myTargetArray[2] = "brown";
      myTargetArray[3] = "fox";
      myTargetArray[4] = "jumps";
      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, ' ');
   }

   public static void PrintValues(String[] myArr, char mySeparator)
   {
      for (int i = 0; i < myArr.Length; i++)
         Console.Write($"{mySeparator}{myArr[i]}");
      Console.WriteLine();
   }
}
/*
This code produces the following output.

The target Array contains the following before:
 The quick brown fox jumps over the lazy dog
After copying the keys, starting at index 6:
 The quick brown fox jumps over B A dog
After copying the values, starting at index 6:
 The quick brown fox jumps over valueB valueA dog

*/

Comentarios

Los elementos se copian en en Array el mismo orden en el que el enumerador recorre en iteración .Hashtable

Para copiar solo las claves de Hashtable, use Hashtable.Keys.CopyTo.

Para copiar solo los valores de Hashtable, use Hashtable.Values.CopyTo.

Este método es una O(n) operación, donde n es Count.

Se aplica a

Producto Versiones
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Consulte también