System.Collections.Hashtable.CopyTo 方法

方法描述

将 Hashtable 元素复制到一维 Array 实例中的指定索引位置。

语法定义(C# System.Collections.Hashtable.CopyTo 方法 的用法)

public virtual void CopyTo(
	Array array,
	int arrayIndex
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
array System-Array 一维 Array,它是从 Hashtable 复制的 DictionaryEntry 对象的目标位置。Array 必须具有从零开始的索引。
arrayIndex System-Int32 array 中从零开始的索引,从此索引处开始进行复制。
返回值 void

提示和注释

这些元素将复制到 Array,复制顺序与枚举数循环访问 Hashtable 的顺序一样。

若要只复制 Hashtable 中的键,请使用 Hashtable.Keys.CopyTo。

若要只复制 Hashtable 中的值,请使用 Hashtable.Values.CopyTo。

此方法的运算复杂度为 O(n),其中 n 是 Count。

System.Collections.Hashtable.CopyTo 方法例子

下面的示例说明如何将 Hashtable 中键的列表或值的列表复制到一维 Array 中。

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.
      String[] myTargetArray = new 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, ' ' );
   }

   public static void PrintValues( 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

*/

异常

异常 异常描述
ArgumentNullException array 为 null。
ArgumentOutOfRangeException arrayIndex 小于零。
ArgumentException
  • array 是多维的。
  • 源 Hashtable 中的元素数大于从 arrayIndex 到目标 array 结尾处之间的可用空间。
InvalidCastException 源 Hashtable 的类型无法自动转换为目标 array 的类型。

命名空间

namespace: System.Collections

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。