Array.Copy 方法

定義

將某個 Array 中的項目範圍複製到另一個 Array,並在必要時執行類型轉型和 Boxing。

多載

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

Array 複製某範圍的項目 (從指定的來源索引開始),並且將它們貼至另一個 Array (從指定的目的索引開始)。 長度與索引都指定為 64 位元的整數。

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

Array 複製某範圍的項目 (從指定的來源索引開始),並且將它們貼至另一個 Array (從指定的目的索引開始)。 長度與索引都指定為 32 位元的整數。

Copy(Array, Array, Int32)

Array 複製某範圍的項目 (從第一個項目開始),並且將它們貼至另一個 Array (從第一個項目開始)。 長度已指定為 32 位元整數。

Copy(Array, Array, Int64)

Array 複製某範圍的項目 (從第一個項目開始),並且將它們貼至另一個 Array (從第一個項目開始)。 長度是以 64 位元整數方式指定。

範例

下列程式代碼範例示範如何將類型之一複製到另一個 ArrayObjectArray 類型整數的類型。

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
 */
open System

let printValues (myArr: 'a []) =
    let mutable i = 0;
    let cols = myArr.GetLength(myArr.Rank - 1)
    for item in myArr do
        if i < cols then
            i <- i + 1
        else
            printfn ""
            i <- 1
        printf $"\t{item}"
    printfn ""

 // Creates and initializes a new Array of type int.
let myIntArray = [| 1..5 |]

// Creates and initializes a new Array of type Object.
let myObjArray = Array.init 5 (fun i -> i + 26 :> obj)

// Displays the initial values of both arrays.
printfn "int array:"
printValues myIntArray 
printfn "Object array:"
printValues myObjArray

// Copies the first element from the int 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 int array.
Array.Copy(myObjArray, myObjArray.GetUpperBound 0 - 1, myIntArray, myIntArray.GetUpperBound 0 - 1, 2)

// Displays the values of the modified arrays.
printfn "int array - Last two elements should now be the same as Object array:"
printValues myIntArray 
printfn "Object array - First element should now be the same as int array:"
printValues myObjArray


// This code produces the following output.
//     int array:
//         1    2    3    4    5
//     Object array:
//         26    27    28    29    30
//     int 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 int array:
//         1    27    28    29    30
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array of type int.
      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( "int array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array:" );
      PrintValues( myObjArray );

      // Copies the first element from the int 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 int array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Displays the values of the modified arrays.
      Console.WriteLine( "int 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 int 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.

int array:
    1    2    3    4    5
Object array:
    26    27    28    29    30
int 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 int 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

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

Array 複製某範圍的項目 (從指定的來源索引開始),並且將它們貼至另一個 Array (從指定的目的索引開始)。 長度與索引都指定為 64 位元的整數。

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

sourceIndex
Int64

64 位元的整數,代表 sourceArray 中的索引,由此開始複製。

destinationArray
Array

接收資料的 Array

destinationIndex
Int64

64 位元的整數,代表 destinationArray 中的索引,由此開始儲存。

length
Int64

64 位元整數,表示要複製的項目數目。 整數必須介於零和 Int32.MaxValue 之間,包含。

例外狀況

sourceArraynull

-或-

destinationArraynull

sourceArraydestinationArray 的順位不同。

sourceArraydestinationArray 類型不相容。

sourceArray 至少有一個項目無法轉換成 destinationArray 的類型。

sourceIndex 超出 sourceArray 的有效索引範圍。

-或-

destinationIndex 超出 destinationArray 的有效索引範圍。

-或-

length 小於 0 或大於 Int32.MaxValue

length 大於從 sourceIndexsourceArray 結尾的項目數。

-或-

length 大於從 destinationIndexdestinationArray 結尾的項目數。

備註

sourceArraydestinationArray 參數必須具有相同數目的維度。 此外, destinationArray 必須已經維度,而且必須有足夠的元素數目,從 destinationIndex 位置開始容納複製的數據。

在多維度陣列之間複製時,陣列的行為就像長一維陣列,其中數據列 (或數據行) 在概念上配置端對端。 例如,如果陣列有三個數據列 (或數據行) 各有四個元素,則從陣列開頭複製六個元素會複製第一個數據列的所有四個元素 (或數據行) ,以及第二個數據列的前兩個元素 (或數據行) 。 若要從第三個數據列的第二個元素開始複製 (或數據行) , sourceIndex 必須是第一個數據列的上限 (或數據行) 加上第二個數據列的長度 (或數據行) 加上兩個。

如果 sourceArraydestinationArray 重疊,這個方法的行為就如同 在覆寫之前destinationArray,在暫存位置保留的原始值sourceArray一樣。

[C++]

這個方法相當於標準 C/C++ 函式 memmove,而不是 memcpy

陣列可以是參考型別陣列或實值型別數位。 視需要執行類型向下傳播。

  • 從參考型別數位複製到實值型別數組時,會取消收件匣,然後複製每個元素。 從實值型別數位複製到參考型別數位時,會 Boxed 並複製每個元素。

  • 從 reference-type 或 value-type 陣列複製到陣列 Object 時,會建立 來 Object 保存每個值或參考,然後複製 。 從 Object 陣列複製到 reference-type 或 value-type 陣列,而且無法指派時, InvalidCastException 會擲回 。

  • 如果 sourceArraydestinationArray 都是參考型別陣列,或是型 Object別的兩個陣列,則會執行淺層複製。 的 Array 淺層複本是新的 Array ,其中包含與原始 Array相同的項目參考。 不會複製專案本身或專案所參考的任何專案。 相反地,的 Array 深層複本會複製專案,以及專案直接或間接參考的所有專案。

ArrayTypeMismatchException如果陣列的類型不相容,則會擲回 。 類型相容性的定義如下:

  • 類型與本身相容。

  • 實值型別與 Object 實作的介面型別相容,且與該值型別所實作的介面類型相容。 只有在實作該介面時,才會將實值型別視為連接到介面。 中斷連線的類型不相容。

  • 如果從來源類型複製到目的地類型是擴展轉換,則兩個內建 (預先定義的) 實值類型都相容。 擴大轉換永遠不會遺失資訊,而縮小轉換可能會遺失資訊。 例如,將 32 位帶正負號的整數轉換成 64 位帶正負號的整數是擴展轉換,而將 64 位帶正負號的整數轉換成 32 位帶正負號的整數則是縮小轉換。 如需轉換的詳細資訊,請參閱 Convert

  • 非內部 (用戶定義) 實值類型只與本身相容。

  • 列舉會隱含地轉換為 Enum 其基礎類型,以及其基礎類型。

例如,如果中的每個 sourceArray 專案都需要向下播 (,從基類到衍生類別,或從介面到物件) ,而且無法將一或多個項目轉換成 中的 destinationArray對應類型, InvalidCastException 則會擲回 。

如果此方法在複製時擲回例外狀況,則 的狀態 destinationArray 為未定義。

這個方法是 O (n) 作業,其中 nlength

另請參閱

適用於

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

Array 複製某範圍的項目 (從指定的來源索引開始),並且將它們貼至另一個 Array (從指定的目的索引開始)。 長度與索引都指定為 32 位元的整數。

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

sourceIndex
Int32

32 位元的整數,代表 sourceArray 中的索引,由此開始複製。

destinationArray
Array

接收資料的 Array

destinationIndex
Int32

32 位元的整數,代表 destinationArray 中的索引,由此開始儲存。

length
Int32

32 位元整數,表示要複製的項目數目。

例外狀況

sourceArraynull

-或-

destinationArraynull

sourceArraydestinationArray 的順位不同。

sourceArraydestinationArray 類型不相容。

sourceArray 至少有一個項目無法轉換成 destinationArray 的類型。

sourceIndex 小於 sourceArray 的第一個維度下限。

-或-

destinationIndex 小於 destinationArray 的第一個維度下限。

-或-

length 小於零。

length 大於從 sourceIndexsourceArray 結尾的項目數。

-或-

length 大於從 destinationIndexdestinationArray 結尾的項目數。

備註

sourceArraydestinationArray 參數必須具有相同數目的維度。 此外, destinationArray 必須已經維度,而且必須有足夠的元素數目,從 destinationIndex 位置開始容納複製的數據。

在多維度陣列之間複製時,陣列的行為就像長一維陣列,其中數據列 (或數據行) 在概念上配置端對端。 例如,如果陣列有三個數據列 (或數據行) 各有四個元素,則從陣列開頭複製六個元素會複製第一個數據列的所有四個元素 (或數據行) ,以及第二個數據列的前兩個元素 (或數據行) 。 若要從第三個數據列的第二個元素開始複製 (或數據行) , sourceIndex 必須是第一個數據列的上限 (或數據行) 加上第二個數據列的長度 (或數據行) 加上兩個。

如果 sourceArraydestinationArray 重疊,這個方法的行為就如同 在覆寫之前destinationArray,在暫存位置保留的原始值sourceArray一樣。

[C++]

這個方法相當於標準 C/C++ 函式 memmove,而不是 memcpy

陣列可以是參考型別陣列或實值型別數位。 視需要執行類型向下傳播。

  • 從參考型別數位複製到實值型別數組時,會取消收件匣,然後複製每個元素。 從實值型別數位複製到參考型別數位時,會 Boxed 並複製每個元素。

  • 從 reference-type 或 value-type 陣列複製到陣列 Object 時,會建立 來 Object 保存每個值或參考,然後複製 。 從 Object 陣列複製到 reference-type 或 value-type 陣列,而且無法指派時, InvalidCastException 會擲回 。

  • 如果 sourceArraydestinationArray 都是參考型別陣列,或是型 Object別的兩個陣列,則會執行淺層複製。 的 Array 淺層複本是新的 Array ,其中包含與原始 Array相同的項目參考。 不會複製專案本身或專案所參考的任何專案。 相反地,的 Array 深層複本會複製專案,以及專案直接或間接參考的所有專案。

ArrayTypeMismatchException如果陣列的類型不相容,則會擲回 。 類型相容性的定義如下:

  • 類型與本身相容。

  • 實值型別與 Object 實作的介面型別相容,且與該值型別所實作的介面類型相容。 只有在實作該介面時,才會將實值型別視為連接到介面。 中斷連線的類型不相容。

  • 如果從來源類型複製到目的地類型是擴展轉換,則兩個內建 (預先定義的) 實值類型都相容。 擴大轉換永遠不會遺失資訊,而縮小轉換可能會遺失資訊。 例如,將 32 位帶正負號的整數轉換成 64 位帶正負號的整數是擴展轉換,而將 64 位帶正負號的整數轉換成 32 位帶正負號的整數則是縮小轉換。 如需轉換的詳細資訊,請參閱 Convert

  • 非內部 (用戶定義) 實值類型只與本身相容。

  • 列舉會隱含地轉換為 Enum 其基礎類型,以及其基礎類型。

例如,如果中的每個 sourceArray 專案都需要向下播 (,從基類到衍生類別,或從介面到物件) ,而且無法將一或多個項目轉換成 中的 destinationArray對應類型, InvalidCastException 則會擲回 。

如果此方法在複製時擲回例外狀況,則 的狀態 destinationArray 為未定義。

這個方法是 O (n) 作業,其中 nlength

另請參閱

適用於

Copy(Array, Array, Int32)

Array 複製某範圍的項目 (從第一個項目開始),並且將它們貼至另一個 Array (從第一個項目開始)。 長度已指定為 32 位元整數。

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

destinationArray
Array

接收資料的 Array

length
Int32

32 位元整數,表示要複製的項目數目。

例外狀況

sourceArraynull

-或-

destinationArraynull

sourceArraydestinationArray 的順位不同。

sourceArraydestinationArray 類型不相容。

sourceArray 至少有一個項目無法轉換成 destinationArray 的類型。

length 小於零。

length 大於 sourceArray 的項目數。

-或-

length 大於 destinationArray 的項目數。

備註

sourceArraydestinationArray 參數必須具有相同數目的維度。 此外,必須已經建立維度, destinationArray 而且必須有足夠的元素數目,才能容納複製的數據。

在多維度陣列之間複製時,陣列的行為就像長一維陣列,其中數據列 (或數據行) 在概念上以端對端方式配置。 例如,如果陣列有三個數據列 (或數據行) 各有四個元素,則從陣列開頭複製六個元素會複製第一個數據列的所有四個元素 (或數據行) ,以及第二個數據列的前兩個元素 (或數據行) 。

如果 sourceArraydestinationArray 重疊,這個方法的行為就如同 在覆寫之前destinationArray,在暫存位置保留的原始值sourceArray一樣。

[C++]

這個方法相當於標準 C/C++ 函式 memmove,而不是 memcpy

陣列可以是參考型別陣列或實值型別數位。 視需要執行類型向下傳播。

  • 從參考型別數位複製到實值型別數組時,會取消收件匣,然後複製每個元素。 從實值型別數位複製到參考型別數位時,會 Boxed 並複製每個元素。

  • 從 reference-type 或 value-type 陣列複製到陣列 Object 時,會建立 來 Object 保存每個值或參考,然後複製 。 從 Object 陣列複製到 reference-type 或 value-type 陣列,而且無法指派時, InvalidCastException 會擲回 。

  • 如果 sourceArraydestinationArray 都是參考型別陣列,或是型 Object別的兩個陣列,則會執行淺層複製。 的 Array 淺層複本是新的 Array ,其中包含與原始 Array相同的項目參考。 不會複製專案本身或專案所參考的任何專案。 相反地,的 Array 深層複本會複製專案,以及專案直接或間接參考的所有專案。

ArrayTypeMismatchException如果陣列的類型不相容,則會擲回 。 類型相容性的定義如下:

  • 類型與本身相容。

  • 實值型別與 Object 實作的介面型別相容,且與該值型別所實作的介面類型相容。 只有在實作該介面時,才會將實值型別視為連接到介面。 中斷連線的類型不相容。

  • 如果從來源類型複製到目的地類型是擴展轉換,則兩個內建 (預先定義的) 實值類型都相容。 擴大轉換永遠不會遺失資訊,而縮小轉換可能會遺失資訊。 例如,將 32 位帶正負號的整數轉換成 64 位帶正負號的整數是擴展轉換,而將 64 位帶正負號的整數轉換成 32 位帶正負號的整數則是縮小轉換。 如需轉換的詳細資訊,請參閱 Convert

  • 非內部 (用戶定義) 實值類型只與本身相容。

  • 列舉會隱含地轉換為 Enum 其基礎類型,以及其基礎類型。

例如,如果中的每個 sourceArray 專案都需要向下播 (,從基類到衍生類別,或從介面到物件) ,而且無法將一或多個項目轉換成 中的 destinationArray對應類型, InvalidCastException 則會擲回 。

如果此方法在複製時擲回例外狀況,則 的狀態 destinationArray 為未定義。

這個方法是 O (n) 作業,其中 nlength

另請參閱

適用於

Copy(Array, Array, Int64)

Array 複製某範圍的項目 (從第一個項目開始),並且將它們貼至另一個 Array (從第一個項目開始)。 長度是以 64 位元整數方式指定。

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

destinationArray
Array

接收資料的 Array

length
Int64

64 位元整數,表示要複製的項目數目。 整數必須介於零和 Int32.MaxValue 之間,包含。

例外狀況

sourceArraynull

-或-

destinationArraynull

sourceArraydestinationArray 的順位不同。

sourceArraydestinationArray 類型不相容。

sourceArray 至少有一個項目無法轉換成 destinationArray 的類型。

length 小於 0 或大於 Int32.MaxValue

length 大於 sourceArray 的項目數。

-或-

length 大於 destinationArray 的項目數。

備註

sourceArraydestinationArray 參數必須具有相同數目的維度。 此外,必須已經建立維度, destinationArray 而且必須有足夠的元素數目,才能容納複製的數據。

在多維度陣列之間複製時,陣列的行為就像長一維陣列,其中數據列 (或數據行) 在概念上以端對端方式配置。 例如,如果陣列有三個數據列 (或數據行) 各有四個元素,則從陣列開頭複製六個元素會複製第一個數據列的所有四個元素 (或數據行) ,以及第二個數據列的前兩個元素 (或數據行) 。

如果 sourceArraydestinationArray 重疊,這個方法的行為就如同 在覆寫之前destinationArray,在暫存位置保留的原始值sourceArray一樣。

[C++]

這個方法相當於標準 C/C++ 函式 memmove,而不是 memcpy

陣列可以是參考型別陣列或實值型別數位。 視需要執行類型向下傳播。

  • 從參考型別數位複製到實值型別數組時,會取消收件匣,然後複製每個元素。 從實值型別數位複製到參考型別數位時,會針對每個元素進行 Boxed 並複製。

  • 從引用類型或實值型別數位複製 Object 到陣列時,會建立 來 Object 保存每個值或參考,然後複製 。 從 Object 陣列複製到參考型別或實值型別陣列,而且無法執行指派時, InvalidCastException 會擲回 。

  • 如果 sourceArraydestinationArray 都是參考型別陣列,或是兩種型 Object別的陣列,則會執行淺層複製。 的 Array 淺層複本是新的 Array ,其中包含與原始 Array相同的項目參考。 不會複製專案本身或專案所參考的任何專案。 相反地,的深層複本 Array 會複製元素,以及專案直接或間接參考的所有專案。

ArrayTypeMismatchException如果陣列的類型不相容,則會擲回 。 類型相容性的定義如下:

  • 類型與本身相容。

  • 實值型別與 Object 該實值型別所實作的介面類型相容。 只有在實作該介面時,才會將實值型別視為連接到介面。 中斷連線的類型不相容。

  • 如果從來源類型複製到目的地類型是擴展轉換,則兩個內建 (預先定義的) 實值類型相容。 擴展轉換永遠不會遺失資訊,而縮小轉換可能會失去資訊。 例如,將 32 位帶正負號的整數轉換成 64 位帶正負號的整數是擴展轉換,而將 64 位帶正負號的整數轉換成 32 位帶正負號的整數則是縮小轉換。 如需轉換的詳細資訊,請參閱 Convert

  • 非內部 (用戶定義) 實值型別只與本身相容。

  • 列舉具有基礎類型的隱含轉換 Enum 和 。

例如,如果中的每個 sourceArray 元素都需要向下播 (,從基類到衍生類別,或從介面轉換成物件) ,而且一或多個元素無法轉換成 中的 destinationArray對應類型, InvalidCastException 則會擲回 。

如果此方法在複製時擲回例外狀況,則 的狀態 destinationArray 為未定義。

這個方法是 O (n) 作業,其中 nlength

另請參閱

適用於