Array.Copy 方法

定义

将一个 Array 的一部分元素复制到另一个 Array 中,并根据需要执行类型转换和装箱。Copies a range of elements in one Array to another Array and performs type casting and boxing as required.

重载

Copy(Array, Int64, Array, Int64, Int64)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. 长度和索引指定为 64 位整数。The length and the indexes are specified as 64-bit integers.

Copy(Array, Int32, Array, Int32, Int32)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. 长度和索引指定为 32 位整数。The length and the indexes are specified as 32-bit integers.

Copy(Array, Array, Int64)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. 长度指定为 64 位整数。The length is specified as a 64-bit integer.

Copy(Array, Array, Int32)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. 长度指定为 32 位整数。The length is specified as a 32-bit integer.

Copy(Array, Int64, Array, Int64, Int64)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. 长度和索引指定为 64 位整数。The length and the indexes are specified as 64-bit integers.

public:
 static void Copy(Array ^ sourceArray, long sourceIndex, Array ^ destinationArray, long destinationIndex, long length);
public static void Copy (Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length);
static member Copy : Array * int64 * Array * int64 * int64 -> unit
Public Shared Sub Copy (sourceArray As Array, sourceIndex As Long, destinationArray As Array, destinationIndex As Long, length As Long)

参数

sourceArray
Array

包含要复制的数据的 ArrayThe Array that contains the data to copy.

sourceIndex
Int64

一个 64 位整数,它表示复制开始处的 sourceArray 中的索引。A 64-bit integer that represents the index in the sourceArray at which copying begins.

destinationArray
Array

接收数据的 ArrayThe Array that receives the data.

destinationIndex
Int64

一个 64 位整数,它表示存储开始处的 destinationArray 中的索引。A 64-bit integer that represents the index in the destinationArray at which storing begins.

length
Int64

一个 64 位整数,它表示要复制的元素数目。A 64-bit integer that represents the number of elements to copy. 该整数必须介于零和 MaxValue 之间(包括这两个值)。The integer must be between zero and MaxValue, inclusive.

异常

sourceArraynullsourceArray is null.

-or- destinationArraynulldestinationArray is null.

sourceArraydestinationArray 具有不同的秩。sourceArray and destinationArray have different ranks.

sourceArraydestinationArray 属于不兼容的类型。sourceArray and destinationArray are of incompatible types.

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。At least one element in sourceArray cannot be cast to the type of destinationArray.

sourceIndex 超出了 sourceArray 的有效索引范围。sourceIndex is outside the range of valid indexes for the sourceArray.

-or- destinationIndex 超出了 destinationArray 的有效索引范围。destinationIndex is outside the range of valid indexes for the destinationArray.

-or- length 小于 0 或大于 MaxValuelength is less than 0 or greater than MaxValue.

length 大于从 sourceIndexsourceArray末尾的元素数。length is greater than the number of elements from sourceIndex to the end of sourceArray.

-or- length 大于从 destinationIndexdestinationArray 末尾的元素数。length is greater than the number of elements from destinationIndex to the end of destinationArray.

示例

下面的代码示例演示如何将类型 Object 的一 Array 复制到整数类型的另一个 ArrayThe following code example shows how to copy from one Array of type Object to another Array of type integer.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   
   // Creates and initializes a new Array instance of type Int32.
   Array^ myIntArray = Array::CreateInstance( Type::GetType(  "System.Int32" ), 5 );
   for ( int i = myIntArray->GetLowerBound( 0 ); i <= myIntArray->GetUpperBound( 0 ); i++ )
      myIntArray->SetValue( i + 1, i );
   
   // Creates and initializes a new Array instance of type Object.
   Array^ myObjArray = Array::CreateInstance( Type::GetType(  "System.Object" ), 5 );
   for ( int i = myObjArray->GetLowerBound( 0 ); i <= myObjArray->GetUpperBound( 0 ); i++ )
      myObjArray->SetValue( i + 26, i );
   
   // Displays the initial values of both arrays.
   Console::WriteLine(  "Int32 array:" );
   PrintValues( myIntArray );
   Console::WriteLine(  "Object array:" );
   PrintValues( myObjArray );
   
   // Copies the first element from the Int32 array to the Object array.
   Array::Copy( myIntArray, myIntArray->GetLowerBound( 0 ), myObjArray, myObjArray->GetLowerBound( 0 ), 1 );
   
   // Copies the last two elements from the Object array to the Int32 array.
   Array::Copy( myObjArray, myObjArray->GetUpperBound( 0 ) - 1, myIntArray, myIntArray->GetUpperBound( 0 ) - 1, 2 );
   
   // Displays the values of the modified arrays.
   Console::WriteLine(  "Int32 array - Last two elements should now be the same as Object array:" );
   PrintValues( myIntArray );
   Console::WriteLine(  "Object array - First element should now be the same as Int32 array:" );
   PrintValues( myObjArray );
}

void PrintValues( Array^ myArr )
{
   System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
   int i = 0;
   int cols = myArr->GetLength( myArr->Rank - 1 );
   while ( myEnumerator->MoveNext() )
   {
      if ( i < cols )
      {
         i++;
      }
      else
      {
         Console::WriteLine();
         i = 1;
      }

      Console::Write(  "\t{0}", myEnumerator->Current );
   }

   Console::WriteLine();
}

/*
 This code produces the following output.
 
 Int32 array:
     1    2    3    4    5
 Object array:
     26    27    28    29    30
 Int32 array - Last two elements should now be the same as Object array:
     1    2    3    29    30
 Object array - First element should now be the same as Int32 array:
     1    27    28    29    30
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array of type Int32.
      Array myIntArray=Array.CreateInstance( typeof(System.Int32), 5 );
      for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
         myIntArray.SetValue( i+1, i );

      // Creates and initializes a new Array of type Object.
      Array myObjArray = Array.CreateInstance( typeof(System.Object), 5 );
      for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
         myObjArray.SetValue( i+26, i );

      // Displays the initial values of both arrays.
      Console.WriteLine( "Int32 array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array:" );
      PrintValues( myObjArray );

      // Copies the first element from the Int32 array to the Object array.
      Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );

      // Copies the last two elements from the Object array to the Int32 array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Displays the values of the modified arrays.
      Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
      PrintValues( myObjArray );
   }


   public static void PrintValues( Array myArr )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

Int32 array:
    1    2    3    4    5
Object array:
    26    27    28    29    30
Int32 array - Last two elements should now be the same as Object array:
    1    2    3    29    30
Object array - First element should now be the same as Int32 array:
    1    27    28    29    30
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array of type Int32.
        Dim myIntArray As Array = _
           Array.CreateInstance(GetType(System.Int32), 5)
        Dim i As Integer
        For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
            myIntArray.SetValue(i + 1, i)
        Next i 
        ' Creates and initializes a new Array of type Object.
        Dim myObjArray As Array = _
           Array.CreateInstance(GetType(System.Object), 5)
        For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
            myObjArray.SetValue(i + 26, i)
        Next i 
        ' Displays the initial values of both arrays.
        Console.WriteLine("Int32 array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array:")
        PrintValues(myObjArray)
        
        ' Copies the first element from the Int32 array to the Object array.
        Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
           myObjArray.GetLowerBound(0), 1)
        
        ' Copies the last two elements from the Object array to the Int32 array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
           myIntArray.GetUpperBound(0) - 1, 2)
        
        ' Displays the values of the modified arrays.
        Console.WriteLine("Int32 array - Last two elements should now be " _
           + "the same as Object array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array - First element should now be the " _
           + "same as Int32 array:")
        PrintValues(myObjArray)
    End Sub
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Int32 array:
'     1    2    3    4    5
' Object array:
'     26    27    28    29    30
' Int32 array - Last two elements should now be the same as Object array:
'     1    2    3    29    30
' Object array - First element should now be the same as Int32 array:
'     1    27    28    29    30

注解

sourceArraydestinationArray 参数必须具有相同的维数。The sourceArray and destinationArray parameters must have the same number of dimensions. 此外,destinationArray 必须已设置了维度,并且必须从 destinationIndex 位置开始有足够数量的元素,才能容纳复制的数据。In addition, destinationArray must already have been dimensioned and must have a sufficient number of elements starting from the destinationIndex position to accommodate the copied data.

在多维数组之间复制时,数组的行为类似于一维数组,其中的行(或列)在概念上是端到端的布局。When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end-to-end. 例如,如果一个数组有三行(或两列),其中每个行都有四个元素,则从该数组的开头复制六个元素将复制第一行(或列)的所有四个元素和第二行(或列)的前两个元素。For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column). 若要从第三行(或列)的第二个元素开始复制,sourceIndex 必须是第一行(或列)的上限加上第二行(或列)加两行的长度。To start copying from the second element of the third row (or column), sourceIndex must be the upper bound of the first row (or column) plus the length of the second row (or column) plus two.

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖 destinationArray 之前,在临时位置保留 sourceArray 的原始值。If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.

[C++][C++]

此方法等效于标准 C/C++函数 memmove,而不是 memcpyThis method is equivalent to the standard C/C++ function memmove, not memcpy.

数组可以是引用类型的数组或值类型数组。The arrays can be reference-type arrays or value-type arrays. 根据需要执行类型 downcasting。Type downcasting is performed, as required.

  • 从引用类型数组复制到值类型数组时,每个元素都将取消装箱,然后复制。When copying from a reference-type array to a value-type array, each element is unboxed and then copied. 从值类型的数组复制到引用类型的数组时,每个元素都进行装箱,然后复制。When copying from a value-type array to a reference-type array, each element is boxed and then copied.

  • 从引用类型或值类型数组复制到 Object 数组时,将创建一个 Object 来保存每个值或引用,然后将其复制。When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Object 数组复制到引用类型或值类型数组时,如果不可能进行赋值,则会引发 InvalidCastExceptionWhen copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果 sourceArraydestinationArray 均为引用类型数组,或是 Object类型的数组,则执行浅表复制。If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Array 的浅表副本是新 Array,它包含对与原始 Array相同的元素的引用。A shallow copy of an Array is a new Array containing references to the same elements as the original Array. 不复制元素本身或元素所引用的任何内容。The elements themselves or anything referenced by the elements are not copied. 与此相反,Array 的深层副本会复制元素以及元素直接或间接引用的所有内容。In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

如果数组的类型不兼容,则会引发 ArrayTypeMismatchExceptionAn ArrayTypeMismatchException is thrown if the arrays are of incompatible types. 类型兼容性定义如下:Type compatibility is defined as follows:

  • 类型与自身兼容。A type is compatible with itself.

  • 值类型与 Object 以及由该值类型实现的接口类型兼容。A value type is compatible with Object and with an interface type implemented by that value type. 仅当某个值类型直接实现该接口时,才会将该值类型视为已连接。A value type is considered connected to an interface only if it implements that interface directly. 断开连接的类型不兼容。Disconnected types are not compatible.

  • 如果从源类型到目标类型的复制是扩大转换,则两个内部(预定义)值类型是兼容的。Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. 扩大转换从不会丢失信息,而收缩转换可能会丢失信息。A widening conversion never loses information, whereas a narrowing conversion can lose information. 例如,将32位有符号整数转换为64位有符号整数是扩大转换,将64位有符号整数转换为32位带符号整数是收缩转换。For example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. 有关转换的详细信息,请参阅 ConvertFor more information about conversions, see Convert.

  • Nonintrinsic (用户定义)值类型仅与自身兼容。A nonintrinsic (user-defined) value type is compatible only with itself.

  • 枚举具有到 Enum 及其基础类型的隐式转换。Enumerations have an implicit conversion to Enum and to their underlying type.

如果 sourceArray 中的每个元素都需要向下转换(例如,从基类到派生类或从接口到对象),并且不能将一个或多个元素强制转换为 destinationArray中的相应类型,则会引发 InvalidCastExceptionIf every element in sourceArray requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray, an InvalidCastException is thrown.

如果此方法在复制时引发异常,则 destinationArray 的状态为 undefined。If this method throws an exception while copying, the state of destinationArray is undefined.

此方法的运算复杂度为 O (n),其中 n lengthThis method is an O(n) operation, where n is length.

另请参阅

Copy(Array, Int32, Array, Int32, Int32)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. 长度和索引指定为 32 位整数。The length and the indexes are specified as 32-bit integers.

public:
 static void Copy(Array ^ sourceArray, int sourceIndex, Array ^ destinationArray, int destinationIndex, int length);
public static void Copy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
static member Copy : Array * int * Array * int * int -> unit
Public Shared Sub Copy (sourceArray As Array, sourceIndex As Integer, destinationArray As Array, destinationIndex As Integer, length As Integer)

参数

sourceArray
Array

包含要复制的数据的 ArrayThe Array that contains the data to copy.

sourceIndex
Int32

一个 32 位整数,它表示 sourceArray 中复制开始处的索引。A 32-bit integer that represents the index in the sourceArray at which copying begins.

destinationArray
Array

接收数据的 ArrayThe Array that receives the data.

destinationIndex
Int32

一个 32 位整数,它表示 destinationArray 中存储开始处的索引。A 32-bit integer that represents the index in the destinationArray at which storing begins.

length
Int32

一个 32 位整数,它表示要复制的元素数目。A 32-bit integer that represents the number of elements to copy.

异常

sourceArraynullsourceArray is null.

-or- destinationArraynulldestinationArray is null.

sourceArraydestinationArray 具有不同的秩。sourceArray and destinationArray have different ranks.

sourceArraydestinationArray 属于不兼容的类型。sourceArray and destinationArray are of incompatible types.

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。At least one element in sourceArray cannot be cast to the type of destinationArray.

sourceIndex 少于 sourceArray 的第一个维度的下限。sourceIndex is less than the lower bound of the first dimension of sourceArray.

-or- destinationIndex 少于 destinationArray 的第一个维度的下限。destinationIndex is less than the lower bound of the first dimension of destinationArray.

-or- length 小于零。length is less than zero.

length 大于从 sourceIndexsourceArray 末尾的元素数。length is greater than the number of elements from sourceIndex to the end of sourceArray.

-or- length 大于从 destinationIndexdestinationArray 末尾的元素数。length is greater than the number of elements from destinationIndex to the end of destinationArray.

示例

下面的代码示例演示如何将类型 Object 的一 Array 复制到整数类型的另一个 ArrayThe following code example shows how to copy from one Array of type Object to another Array of type integer.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   
   // Creates and initializes a new Array instance of type Int32.
   Array^ myIntArray = Array::CreateInstance( Type::GetType(  "System.Int32" ), 5 );
   for ( int i = myIntArray->GetLowerBound( 0 ); i <= myIntArray->GetUpperBound( 0 ); i++ )
      myIntArray->SetValue( i + 1, i );
   
   // Creates and initializes a new Array instance of type Object.
   Array^ myObjArray = Array::CreateInstance( Type::GetType(  "System.Object" ), 5 );
   for ( int i = myObjArray->GetLowerBound( 0 ); i <= myObjArray->GetUpperBound( 0 ); i++ )
      myObjArray->SetValue( i + 26, i );
   
   // Displays the initial values of both arrays.
   Console::WriteLine(  "Int32 array:" );
   PrintValues( myIntArray );
   Console::WriteLine(  "Object array:" );
   PrintValues( myObjArray );
   
   // Copies the first element from the Int32 array to the Object array.
   Array::Copy( myIntArray, myIntArray->GetLowerBound( 0 ), myObjArray, myObjArray->GetLowerBound( 0 ), 1 );
   
   // Copies the last two elements from the Object array to the Int32 array.
   Array::Copy( myObjArray, myObjArray->GetUpperBound( 0 ) - 1, myIntArray, myIntArray->GetUpperBound( 0 ) - 1, 2 );
   
   // Displays the values of the modified arrays.
   Console::WriteLine(  "Int32 array - Last two elements should now be the same as Object array:" );
   PrintValues( myIntArray );
   Console::WriteLine(  "Object array - First element should now be the same as Int32 array:" );
   PrintValues( myObjArray );
}

void PrintValues( Array^ myArr )
{
   System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
   int i = 0;
   int cols = myArr->GetLength( myArr->Rank - 1 );
   while ( myEnumerator->MoveNext() )
   {
      if ( i < cols )
      {
         i++;
      }
      else
      {
         Console::WriteLine();
         i = 1;
      }

      Console::Write(  "\t{0}", myEnumerator->Current );
   }

   Console::WriteLine();
}

/*
 This code produces the following output.
 
 Int32 array:
     1    2    3    4    5
 Object array:
     26    27    28    29    30
 Int32 array - Last two elements should now be the same as Object array:
     1    2    3    29    30
 Object array - First element should now be the same as Int32 array:
     1    27    28    29    30
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array of type Int32.
      Array myIntArray=Array.CreateInstance( typeof(System.Int32), 5 );
      for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
         myIntArray.SetValue( i+1, i );

      // Creates and initializes a new Array of type Object.
      Array myObjArray = Array.CreateInstance( typeof(System.Object), 5 );
      for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
         myObjArray.SetValue( i+26, i );

      // Displays the initial values of both arrays.
      Console.WriteLine( "Int32 array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array:" );
      PrintValues( myObjArray );

      // Copies the first element from the Int32 array to the Object array.
      Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );

      // Copies the last two elements from the Object array to the Int32 array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Displays the values of the modified arrays.
      Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
      PrintValues( myObjArray );
   }


   public static void PrintValues( Array myArr )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

Int32 array:
    1    2    3    4    5
Object array:
    26    27    28    29    30
Int32 array - Last two elements should now be the same as Object array:
    1    2    3    29    30
Object array - First element should now be the same as Int32 array:
    1    27    28    29    30
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array of type Int32.
        Dim myIntArray As Array = _
           Array.CreateInstance(GetType(System.Int32), 5)
        Dim i As Integer
        For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
            myIntArray.SetValue(i + 1, i)
        Next i 
        ' Creates and initializes a new Array of type Object.
        Dim myObjArray As Array = _
           Array.CreateInstance(GetType(System.Object), 5)
        For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
            myObjArray.SetValue(i + 26, i)
        Next i 
        ' Displays the initial values of both arrays.
        Console.WriteLine("Int32 array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array:")
        PrintValues(myObjArray)
        
        ' Copies the first element from the Int32 array to the Object array.
        Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
           myObjArray.GetLowerBound(0), 1)
        
        ' Copies the last two elements from the Object array to the Int32 array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
           myIntArray.GetUpperBound(0) - 1, 2)
        
        ' Displays the values of the modified arrays.
        Console.WriteLine("Int32 array - Last two elements should now be " _
           + "the same as Object array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array - First element should now be the " _
           + "same as Int32 array:")
        PrintValues(myObjArray)
    End Sub
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Int32 array:
'     1    2    3    4    5
' Object array:
'     26    27    28    29    30
' Int32 array - Last two elements should now be the same as Object array:
'     1    2    3    29    30
' Object array - First element should now be the same as Int32 array:
'     1    27    28    29    30

注解

sourceArraydestinationArray 参数必须具有相同的维数。The sourceArray and destinationArray parameters must have the same number of dimensions. 此外,destinationArray 必须已设置了维度,并且必须从 destinationIndex 位置开始有足够数量的元素,才能容纳复制的数据。In addition, destinationArray must already have been dimensioned and must have a sufficient number of elements starting from the destinationIndex position to accommodate the copied data.

在多维数组之间复制时,数组的行为类似于一维数组,其中的行(或列)在概念上是端到端的布局。When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end-to-end. 例如,如果一个数组有三行(或两列),其中每个行都有四个元素,则从该数组的开头复制六个元素将复制第一行(或列)的所有四个元素和第二行(或列)的前两个元素。For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column). 若要从第三行(或列)的第二个元素开始复制,sourceIndex 必须是第一行(或列)的上限加上第二行(或列)加两行的长度。To start copying from the second element of the third row (or column), sourceIndex must be the upper bound of the first row (or column) plus the length of the second row (or column) plus two.

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖 destinationArray 之前,在临时位置保留 sourceArray 的原始值。If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.

[C++][C++]

此方法等效于标准 C/C++函数 memmove,而不是 memcpyThis method is equivalent to the standard C/C++ function memmove, not memcpy.

数组可以是引用类型的数组或值类型数组。The arrays can be reference-type arrays or value-type arrays. 根据需要执行类型 downcasting。Type downcasting is performed, as required.

  • 从引用类型数组复制到值类型数组时,每个元素都将取消装箱,然后复制。When copying from a reference-type array to a value-type array, each element is unboxed and then copied. 从值类型的数组复制到引用类型的数组时,每个元素都进行装箱,然后复制。When copying from a value-type array to a reference-type array, each element is boxed and then copied.

  • 从引用类型或值类型数组复制到 Object 数组时,将创建一个 Object 来保存每个值或引用,然后将其复制。When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Object 数组复制到引用类型或值类型数组时,如果不可能进行赋值,则会引发 InvalidCastExceptionWhen copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果 sourceArraydestinationArray 均为引用类型数组,或是 Object类型的数组,则执行浅表复制。If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Array 的浅表副本是新 Array,它包含对与原始 Array相同的元素的引用。A shallow copy of an Array is a new Array containing references to the same elements as the original Array. 不复制元素本身或元素所引用的任何内容。The elements themselves or anything referenced by the elements are not copied. 与此相反,Array 的深层副本会复制元素以及元素直接或间接引用的所有内容。In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

如果数组的类型不兼容,则会引发 ArrayTypeMismatchExceptionAn ArrayTypeMismatchException is thrown if the arrays are of incompatible types. 类型兼容性定义如下:Type compatibility is defined as follows:

  • 类型与自身兼容。A type is compatible with itself.

  • 值类型与 Object 以及由该值类型实现的接口类型兼容。A value type is compatible with Object and with an interface type implemented by that value type. 仅当某个值类型直接实现该接口时,才会将该值类型视为已连接。A value type is considered connected to an interface only if it implements that interface directly. 断开连接的类型不兼容。Disconnected types are not compatible.

  • 如果从源类型到目标类型的复制是扩大转换,则两个内部(预定义)值类型是兼容的。Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. 扩大转换从不会丢失信息,而收缩转换可能会丢失信息。A widening conversion never loses information, whereas a narrowing conversion can lose information. 例如,将32位有符号整数转换为64位有符号整数是扩大转换,将64位有符号整数转换为32位带符号整数是收缩转换。For example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. 有关转换的详细信息,请参阅 ConvertFor more information about conversions, see Convert.

  • Nonintrinsic (用户定义)值类型仅与自身兼容。A nonintrinsic (user-defined) value type is compatible only with itself.

  • 枚举具有到 Enum 及其基础类型的隐式转换。Enumerations have an implicit conversion to Enum and to their underlying type.

如果 sourceArray 中的每个元素都需要向下转换(例如,从基类到派生类或从接口到对象),并且不能将一个或多个元素强制转换为 destinationArray中的相应类型,则会引发 InvalidCastExceptionIf every element in sourceArray requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray, an InvalidCastException is thrown.

如果此方法在复制时引发异常,则 destinationArray 的状态为 undefined。If this method throws an exception while copying, the state of destinationArray is undefined.

此方法的运算复杂度为 O (n),其中 n lengthThis method is an O(n) operation, where n is length.

另请参阅

Copy(Array, Array, Int64)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. 长度指定为 64 位整数。The length is specified as a 64-bit integer.

public:
 static void Copy(Array ^ sourceArray, Array ^ destinationArray, long length);
public static void Copy (Array sourceArray, Array destinationArray, long length);
static member Copy : Array * Array * int64 -> unit
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Long)

参数

sourceArray
Array

包含要复制的数据的 ArrayThe Array that contains the data to copy.

destinationArray
Array

接收数据的 ArrayThe Array that receives the data.

length
Int64

一个 64 位整数,它表示要复制的元素数目。A 64-bit integer that represents the number of elements to copy. 该整数必须介于零和 MaxValue 之间(包括这两个值)。The integer must be between zero and MaxValue, inclusive.

异常

sourceArraynullsourceArray is null.

-or- destinationArraynulldestinationArray is null.

sourceArraydestinationArray 具有不同的秩。sourceArray and destinationArray have different ranks.

sourceArraydestinationArray 属于不兼容的类型。sourceArray and destinationArray are of incompatible types.

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。At least one element in sourceArray cannot be cast to the type of destinationArray.

length 小于 0 或大于 MaxValuelength is less than 0 or greater than MaxValue.

length 大于 sourceArray 中的元素数。length is greater than the number of elements in sourceArray.

-or- length 大于 destinationArray 中的元素数。length is greater than the number of elements in destinationArray.

注解

sourceArraydestinationArray 参数必须具有相同的维数。The sourceArray and destinationArray parameters must have the same number of dimensions. 此外,destinationArray 必须已设置了维度,并且必须具有足够数量的元素来容纳复制的数据。In addition, destinationArray must already have been dimensioned and must have a sufficient number of elements to accommodate the copied data.

在多维数组之间复制时,数组的行为类似于一维数组,其中的行(或列)在概念上是端到端的布局。When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. 例如,如果一个数组有三行(或两列),其中每个行都有四个元素,则从该数组的开头复制六个元素将复制第一行(或列)的所有四个元素和第二行(或列)的前两个元素。For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖 destinationArray 之前,在临时位置保留 sourceArray 的原始值。If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.

[C++][C++]

此方法等效于标准 C/C++函数 memmove,而不是 memcpyThis method is equivalent to the standard C/C++ function memmove, not memcpy.

数组可以是引用类型的数组或值类型数组。The arrays can be reference-type arrays or value-type arrays. 根据需要执行类型 downcasting。Type downcasting is performed, as required.

  • 从引用类型数组复制到值类型数组时,每个元素都将取消装箱,然后复制。When copying from a reference-type array to a value-type array, each element is unboxed and then copied. 从值类型的数组复制到引用类型的数组时,每个元素都进行装箱,然后复制。When copying from a value-type array to a reference-type array, each element is boxed and then copied.

  • 从引用类型或值类型数组复制到 Object 数组时,将创建一个 Object 来保存每个值或引用,然后将其复制。When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Object 数组复制到引用类型或值类型数组时,如果不可能进行赋值,则会引发 InvalidCastExceptionWhen copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果 sourceArraydestinationArray 均为引用类型数组,或是 Object类型的数组,则执行浅表复制。If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Array 的浅表副本是新 Array,它包含对与原始 Array相同的元素的引用。A shallow copy of an Array is a new Array containing references to the same elements as the original Array. 不复制元素本身或元素所引用的任何内容。The elements themselves or anything referenced by the elements are not copied. 与此相反,Array 的深层副本会复制元素以及元素直接或间接引用的所有内容。In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

如果数组的类型不兼容,则会引发 ArrayTypeMismatchExceptionAn ArrayTypeMismatchException is thrown if the arrays are of incompatible types. 类型兼容性定义如下:Type compatibility is defined as follows:

  • 类型与自身兼容。A type is compatible with itself.

  • 值类型与 Object 以及由该值类型实现的接口类型兼容。A value type is compatible with Object and with an interface type implemented by that value type. 仅当某个值类型直接实现该接口时,才会将该值类型视为已连接。A value type is considered connected to an interface only if it implements that interface directly. 断开连接的类型不兼容。Disconnected types are not compatible.

  • 如果从源类型到目标类型的复制是扩大转换,则两个内部(预定义)值类型是兼容的。Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. 扩大转换从不会丢失信息,而收缩转换可能会丢失信息。A widening conversion never loses information, whereas a narrowing conversion can lose information. 例如,将32位有符号整数转换为64位有符号整数是扩大转换,将64位有符号整数转换为32位带符号整数是收缩转换。For example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. 有关转换的详细信息,请参阅 ConvertFor more information about conversions, see Convert.

  • Nonintrinsic (用户定义)值类型仅与自身兼容。A nonintrinsic (user-defined) value type is compatible only with itself.

  • 枚举具有到 Enum 及其基础类型的隐式转换。Enumerations have an implicit conversion to Enum and to their underlying type.

如果 sourceArray 中的每个元素都需要向下转换(例如,从基类到派生类或从接口到对象),并且不能将一个或多个元素强制转换为 destinationArray中的相应类型,则会引发 InvalidCastExceptionIf every element in sourceArray requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray, an InvalidCastException is thrown.

如果此方法在复制时引发异常,则 destinationArray 的状态为 undefined。If this method throws an exception while copying, the state of destinationArray is undefined.

此方法的运算复杂度为 O (n),其中 n lengthThis method is an O(n) operation, where n is length.

另请参阅

Copy(Array, Array, Int32)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. 长度指定为 32 位整数。The length is specified as a 32-bit integer.

public:
 static void Copy(Array ^ sourceArray, Array ^ destinationArray, int length);
public static void Copy (Array sourceArray, Array destinationArray, int length);
static member Copy : Array * Array * int -> unit
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)

参数

sourceArray
Array

包含要复制的数据的 ArrayThe Array that contains the data to copy.

destinationArray
Array

接收数据的 ArrayThe Array that receives the data.

length
Int32

一个 32 位整数,它表示要复制的元素数目。A 32-bit integer that represents the number of elements to copy.

异常

sourceArraynullsourceArray is null.

-or- destinationArraynulldestinationArray is null.

sourceArraydestinationArray 具有不同的秩。sourceArray and destinationArray have different ranks.

sourceArraydestinationArray 属于不兼容的类型。sourceArray and destinationArray are of incompatible types.

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。At least one element in sourceArray cannot be cast to the type of destinationArray.

length 小于零。length is less than zero.

length 大于 sourceArray 中的元素数。length is greater than the number of elements in sourceArray.

-or- length 大于 destinationArray 中的元素数。length is greater than the number of elements in destinationArray.

注解

sourceArraydestinationArray 参数必须具有相同的维数。The sourceArray and destinationArray parameters must have the same number of dimensions. 此外,destinationArray 必须已设置了维度,并且必须具有足够数量的元素来容纳复制的数据。In addition, destinationArray must already have been dimensioned and must have a sufficient number of elements to accommodate the copied data.

在多维数组之间复制时,数组的行为类似于一维数组,其中的行(或列)在概念上是端到端的布局。When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. 例如,如果一个数组有三行(或两列),其中每个行都有四个元素,则从该数组的开头复制六个元素将复制第一行(或列)的所有四个元素和第二行(或列)的前两个元素。For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖 destinationArray 之前,在临时位置保留 sourceArray 的原始值。If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.

[C++][C++]

此方法等效于标准 C/C++函数 memmove,而不是 memcpyThis method is equivalent to the standard C/C++ function memmove, not memcpy.

数组可以是引用类型的数组或值类型数组。The arrays can be reference-type arrays or value-type arrays. 根据需要执行类型 downcasting。Type downcasting is performed, as required.

  • 从引用类型数组复制到值类型数组时,每个元素都将取消装箱,然后复制。When copying from a reference-type array to a value-type array, each element is unboxed and then copied. 从值类型的数组复制到引用类型的数组时,每个元素都进行装箱,然后复制。When copying from a value-type array to a reference-type array, each element is boxed and then copied.

  • 从引用类型或值类型数组复制到 Object 数组时,将创建一个 Object 来保存每个值或引用,然后将其复制。When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Object 数组复制到引用类型或值类型数组时,如果不可能进行赋值,则会引发 InvalidCastExceptionWhen copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果 sourceArraydestinationArray 均为引用类型数组,或是 Object类型的数组,则执行浅表复制。If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Array 的浅表副本是新 Array,它包含对与原始 Array相同的元素的引用。A shallow copy of an Array is a new Array containing references to the same elements as the original Array. 不复制元素本身或元素所引用的任何内容。The elements themselves or anything referenced by the elements are not copied. 与此相反,Array 的深层副本会复制元素以及元素直接或间接引用的所有内容。In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

如果数组的类型不兼容,则会引发 ArrayTypeMismatchExceptionAn ArrayTypeMismatchException is thrown if the arrays are of incompatible types. 类型兼容性定义如下:Type compatibility is defined as follows:

  • 类型与自身兼容。A type is compatible with itself.

  • 值类型与 Object 以及由该值类型实现的接口类型兼容。A value type is compatible with Object and with an interface type implemented by that value type. 仅当某个值类型直接实现该接口时,才会将该值类型视为已连接。A value type is considered connected to an interface only if it implements that interface directly. 断开连接的类型不兼容。Disconnected types are not compatible.

  • 如果从源类型到目标类型的复制是扩大转换,则两个内部(预定义)值类型是兼容的。Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. 扩大转换从不会丢失信息,而收缩转换可能会丢失信息。A widening conversion never loses information, whereas a narrowing conversion can lose information. 例如,将32位有符号整数转换为64位有符号整数是扩大转换,将64位有符号整数转换为32位带符号整数是收缩转换。For example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. 有关转换的详细信息,请参阅 ConvertFor more information about conversions, see Convert.

  • Nonintrinsic (用户定义)值类型仅与自身兼容。A nonintrinsic (user-defined) value type is compatible only with itself.

  • 枚举具有到 Enum 及其基础类型的隐式转换。Enumerations have an implicit conversion to Enum and to their underlying type.

如果 sourceArray 中的每个元素都需要向下转换(例如,从基类到派生类或从接口到对象),并且不能将一个或多个元素强制转换为 destinationArray中的相应类型,则会引发 InvalidCastExceptionIf every element in sourceArray requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray, an InvalidCastException is thrown.

如果此方法在复制时引发异常,则 destinationArray 的状态为 undefined。If this method throws an exception while copying, the state of destinationArray is undefined.

此方法的运算复杂度为 O (n),其中 n lengthThis method is an O(n) operation, where n is length.

另请参阅

适用于