System.Array.CopyTo 方法 (Array, Int64)

方法描述

将当前一维 Array 的所有元素复制到指定的一维 Array 中(从指定的目标 Array 索引开始)。 索引指定为 64 位整数。

语法定义(C# System.Array.CopyTo 方法 (Array, Int64) 的用法)

[ComVisibleAttribute(false)]
public void CopyTo(
	Array array,
	long index
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
array System-Array 一维 Array,它是从当前 Array 复制的元素的目标位置。
index System-Int64 一个 64 位整数,它表示 array 中复制开始处的索引。
返回值 void

提示和注释

此方法支持 System.Collections.ICollection 接口。 如果未明确要求实现 System.Collections.ICollection,请使用 Copy 以避免额外的间接寻址。

如果此方法在复制时引发异常,则 array 的状态未定义。

此方法的运算复杂度为 O(n),其中 n 是 Length。 它只执行浅表复制。

System.Array.CopyTo 方法 (Array, Int64)例子

请注意,将复制整个源 Array,包括将覆盖目标 Array 中现有元素的空元素。

public class SamplesArray2{

   public static void Main()  {
      // Creates and initializes the source Array.
      Array myArrayZero=Array.CreateInstance( typeof(String), 3 );
      myArrayZero.SetValue( "zero", 0 );
      myArrayZero.SetValue( "one", 1 );

      // Displays the source Array.
      Console.WriteLine( "The array with lower bound=0 contains:" );
      PrintIndexAndValues( myArrayZero );

      // Creates and initializes the target Array.
      int[] myArrLen = { 4 };
      int[] myArrLow = { 2 };
      Array myArrayTwo=Array.CreateInstance( typeof(String), myArrLen, myArrLow );
      myArrayTwo.SetValue( "two", 2 );
      myArrayTwo.SetValue( "three", 3 );
      myArrayTwo.SetValue( "four", 4 );
      myArrayTwo.SetValue( "five", 5 );

      // Displays the target Array.
      Console.WriteLine( "The array with lower bound=2 contains:" );
      PrintIndexAndValues( myArrayTwo );

      // Copies from the array with lower bound=0 to the array with lower bound=2.
      myArrayZero.CopyTo( myArrayTwo, 3 );

      // Displays the modified target Array.
      Console.WriteLine( "\nAfter copying to the target array from index 3:" );
      PrintIndexAndValues( myArrayTwo );
   }


   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/* 
This code produces the following output.

The array with lower bound=0 contains:
    [0]:    zero
    [1]:    one
    [2]:    
The array with lower bound=2 contains:
    [2]:    two
    [3]:    three
    [4]:    four
    [5]:    five

After copying to the target array from index 3:
    [2]:    two
    [3]:    zero
    [4]:    one
    [5]:
*/

异常

异常 异常描述
ArgumentNullException array 为 null。
ArgumentOutOfRangeException index 超出了 array 的有效索引范围。
ArgumentException
  • array 是多维的。
  • 源 Array 中的元素数大于从 index 到目标 array 结尾处之间的可用空间。
ArrayTypeMismatchException 源 Array 的类型无法自动转换为目标 array 的类型。
RankException 源 Array 是多维的。
InvalidCastException 源 Array 中至少有一个元素无法强制转换为目标 array 的类型。

命名空间

namespace: System

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1 .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 系统要求。