Hashtable.CopyTo(Array, Int32) Yöntem

Tanım

Öğeleri belirtilen dizindeki Hashtable tek boyutlu Array bir örneğe kopyalar.

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

Parametreler

array
Array

'den Hashtablekopyalanan nesnelerin hedefi DictionaryEntry olan tek boyutludurArray. sıfır Array tabanlı dizine sahip olmalıdır.

arrayIndex
Int32

array içinde kopyalamanın başladığı sıfır tabanlı dizin.

Uygulamalar

Özel durumlar

array, null değeridir.

arrayIndex, sıfırdan küçüktür.

array çok boyutludur.

-veya-

Kaynaktaki Hashtable öğelerin sayısı, hedef arrayöğesinin sonuna kadar olan kullanılabilir alandan arrayIndex büyüktür.

Kaynağın Hashtable türü otomatik olarak hedef arraytürüne türlenemez.

Örnekler

Aşağıdaki örnekte anahtar listesinin veya içindeki değer listesinin tek boyutlu Arraybir Hashtable içine nasıl kopyalanması gösterilmektedir.

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

*/

Açıklamalar

Öğeler, numaralandırıcının aracılığıyla Hashtableyinelendiği sırayla öğesine kopyalanırArray.

Yalnızca içindeki Hashtableanahtarları kopyalamak için kullanın Hashtable.Keys.CopyTo.

Yalnızca içindeki Hashtabledeğerleri kopyalamak için kullanın Hashtable.Values.CopyTo.

Bu yöntem bir O(n) işlemdir ve burada n şeklindedir Count.

Şunlara uygulanır

Ürün Sürümler
.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

Ayrıca bkz.