Array.Copy Array.Copy Array.Copy Array.Copy Method

定義

將一個 Array 中某範圍的元素複製到另一個 Array,並在必要時執行型別轉換和 Boxing。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) Copy(Array, Int64, Array, Int64, Int64) Copy(Array, Int64, Array, Int64, Int64) 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) Copy(Array, Int32, Array, Int32, Int32) Copy(Array, Int32, Array, Int32, Int32) 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) Copy(Array, Array, Int64) Copy(Array, Array, Int64) 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) Copy(Array, Array, Int32) Copy(Array, Array, Int32) 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) Copy(Array, Int64, Array, Int64, Int64) Copy(Array, Int64, Array, Int64, Int64) 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 Array Array Array

包含要複製資料的 ArrayThe Array that contains the data to copy.

sourceIndex
Int64 Int64 Int64 Int64

64 位元的整數,代表 sourceArray 中的索引,由此開始複製。A 64-bit integer that represents the index in the sourceArray at which copying begins.

destinationArray
Array Array Array Array

接收資料的 ArrayThe Array that receives the data.

destinationIndex
Int64 Int64 Int64 Int64

64 位元的整數,代表 destinationArray 中的索引,由此開始儲存。A 64-bit integer that represents the index in the destinationArray at which storing begins.

length
Int64 Int64 Int64 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.

範例

下列程式碼範例示範如何從類型Array Object的其中一個複製到整數Array類型的另一個。The 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
*/
Imports System
Imports Microsoft.VisualBasic

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.

如果sourceArray sourceArray destinationArraydestinationArray重迭, 這個方法的行為會如同在覆寫之前, 保留在暫存位置中的原始值。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函式, 而不memcpy是。This method is equivalent to the standard C/C++ function memmove, not memcpy.

陣列可以是參考型別陣列或實數值型別陣列。The arrays can be reference-type arrays or value-type arrays. 視需要執行型別向下檢視。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陣列複製到參考型別或實數值型別陣列, 但不能指派時InvalidCastException , 會擲回。When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果sourceArray ObjectdestinationArray都是參考型別陣列, 或同時為類型的陣列, 則會執行淺層複製。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. 如需轉換的詳細資訊, Convert請參閱。For 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的對應類型, InvalidCastException則會擲回。If 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的狀態會是未定義的。If this method throws an exception while copying, the state of destinationArray is undefined.

這個方法是 O (n) 運算, 其中nlengthThis method is an O(n) operation, where n is length.

另請參閱

Copy(Array, Int32, Array, Int32, Int32) Copy(Array, Int32, Array, Int32, Int32) Copy(Array, Int32, Array, Int32, Int32) 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 Array Array Array

包含要複製資料的 ArrayThe Array that contains the data to copy.

sourceIndex
Int32 Int32 Int32 Int32

32 位元的整數,代表 sourceArray 中的索引,由此開始複製。A 32-bit integer that represents the index in the sourceArray at which copying begins.

destinationArray
Array Array Array Array

接收資料的 ArrayThe Array that receives the data.

destinationIndex
Int32 Int32 Int32 Int32

32 位元的整數,代表 destinationArray 中的索引,由此開始儲存。A 32-bit integer that represents the index in the destinationArray at which storing begins.

length
Int32 Int32 Int32 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.

範例

下列程式碼範例示範如何從類型Array Object的其中一個複製到整數Array類型的另一個。The 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
*/
Imports System
Imports Microsoft.VisualBasic

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.

如果sourceArray sourceArray destinationArraydestinationArray重迭, 這個方法的行為會如同在覆寫之前, 保留在暫存位置中的原始值。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函式, 而不memcpy是。This method is equivalent to the standard C/C++ function memmove, not memcpy.

陣列可以是參考型別陣列或實數值型別陣列。The arrays can be reference-type arrays or value-type arrays. 視需要執行型別向下檢視。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陣列複製到參考型別或實數值型別陣列, 但不能指派時InvalidCastException , 會擲回。When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果sourceArray ObjectdestinationArray都是參考型別陣列, 或同時為類型的陣列, 則會執行淺層複製。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. 如需轉換的詳細資訊, Convert請參閱。For 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的對應類型, InvalidCastException則會擲回。If 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的狀態會是未定義的。If this method throws an exception while copying, the state of destinationArray is undefined.

這個方法是 O (n) 運算, 其中nlengthThis method is an O(n) operation, where n is length.

另請參閱

Copy(Array, Array, Int64) Copy(Array, Array, Int64) Copy(Array, Array, Int64) 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 Array Array Array

包含要複製資料的 ArrayThe Array that contains the data to copy.

destinationArray
Array Array Array Array

接收資料的 ArrayThe Array that receives the data.

length
Int64 Int64 Int64 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 大於 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).

如果sourceArray sourceArray destinationArraydestinationArray重迭, 這個方法的行為會如同在覆寫之前, 保留在暫存位置中的原始值。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函式, 而不memcpy是。This method is equivalent to the standard C/C++ function memmove, not memcpy.

陣列可以是參考型別陣列或實數值型別陣列。The arrays can be reference-type arrays or value-type arrays. 視需要執行型別向下檢視。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陣列複製到參考型別或實數值型別陣列, 但不能指派時InvalidCastException , 會擲回。When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果sourceArray ObjectdestinationArray都是參考型別陣列, 或同時為類型的陣列, 則會執行淺層複製。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. 如需轉換的詳細資訊, Convert請參閱。For 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的對應類型, InvalidCastException則會擲回。If 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的狀態會是未定義的。If this method throws an exception while copying, the state of destinationArray is undefined.

這個方法是 O (n) 運算, 其中nlengthThis method is an O(n) operation, where n is length.

另請參閱

Copy(Array, Array, Int32) Copy(Array, Array, Int32) Copy(Array, Array, Int32) 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 Array Array Array

包含要複製資料的 ArrayThe Array that contains the data to copy.

destinationArray
Array Array Array Array

接收資料的 ArrayThe Array that receives the data.

length
Int32 Int32 Int32 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 大於 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).

如果sourceArray sourceArray destinationArraydestinationArray重迭, 這個方法的行為會如同在覆寫之前, 保留在暫存位置中的原始值。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函式, 而不memcpy是。This method is equivalent to the standard C/C++ function memmove, not memcpy.

陣列可以是參考型別陣列或實數值型別陣列。The arrays can be reference-type arrays or value-type arrays. 視需要執行型別向下檢視。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陣列複製到參考型別或實數值型別陣列, 但不能指派時InvalidCastException , 會擲回。When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • 如果sourceArray ObjectdestinationArray都是參考型別陣列, 或同時為類型的陣列, 則會執行淺層複製。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. 如需轉換的詳細資訊, Convert請參閱。For 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的對應類型, InvalidCastException則會擲回。If 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的狀態會是未定義的。If this method throws an exception while copying, the state of destinationArray is undefined.

這個方法是 O (n) 運算, 其中nlengthThis method is an O(n) operation, where n is length.

另請參閱

適用於