Ler em inglês

Partilhar via


Hashtable.CopyTo(Array, Int32) Método

Definição

Copia os elementos Hashtable para uma instância Array unidimensional no índice especificado.

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

Parâmetros

array
Array

O Array unidimensional que é o destino dos objetos DictionaryEntry copiados de Hashtable. O Array deve ter indexação com base em zero.

arrayIndex
Int32

O índice com base em zero em array no qual a cópia começa.

Implementações

Exceções

array é null.

arrayIndex é menor que zero.

array é multidimensional.

- ou -

O número de elementos na origem Hashtable é maior do que o espaço disponível de arrayIndex até o final do array de destino.

O tipo do Hashtable de origem não pode ser automaticamente convertido para o tipo de array de destino.

Exemplos

O exemplo a seguir mostra como copiar a lista de chaves ou a lista de valores em um Hashtable em um 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

*/

Comentários

Os elementos são copiados para o Array na mesma ordem em que o enumerador itera por meio do Hashtable.

Para copiar apenas as chaves no Hashtable, use Hashtable.Keys.CopyTo.

Para copiar apenas os valores no Hashtable, use Hashtable.Values.CopyTo.

Esse método é uma O(n) operação, em que n é Count.

Aplica-se a

Produto Versões
.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

Confira também