Array.ConstrainedCopy(Array, Int32, Array, Int32, Int32) Array.ConstrainedCopy(Array, Int32, Array, Int32, Int32) Array.ConstrainedCopy(Array, Int32, Array, Int32, Int32) Array.ConstrainedCopy(Array, Int32, Array, Int32, Int32) Method

定義

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. 如果此複本未完全成功,則保證所有的變更一定未完成。Guarantees that all changes are undone if the copy does not succeed completely.

public:
 static void ConstrainedCopy(Array ^ sourceArray, int sourceIndex, Array ^ destinationArray, int destinationIndex, int length);
public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
static member ConstrainedCopy : Array * int * Array * int * int -> unit
Public Shared Sub ConstrainedCopy (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.

sourceArray 類型不同於、也非衍生自 destinationArray 類型。The sourceArray type is neither the same as nor derived from the destinationArray type.

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.

備註

sourceArraydestinationArray參數必須有相同的維度數目。The sourceArray and destinationArray parameters must have the same number of dimensions. sourceArray型別必須相同,或衍生自destinationArray類型; 否則ArrayTypeMismatchException就會擲回。The sourceArray type must be the same as or derived from the destinationArray type; otherwise, an ArrayTypeMismatchException is thrown. 不同於CopyConstrainedCopy確認陣列類型的相容性,然後再執行任何作業。Unlike Copy, ConstrainedCopy verifies the compatibility of the array types before performing any operation.

當多維度陣列之間進行複製,陣列的行為如同長的一維陣列,其中的資料列 (或資料行) 在概念上說明端對端。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重疊,這個方法的行為如同的原始值sourceArray已保留在暫存位置之前destinationArray會覆寫。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. 如果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包含相同的項目與原始參考ArrayA 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.

如果此方法會擲回的例外狀況在複製時destinationArray會保持不變; 因此,ConstrainedCopy可用於限制的執行區域 (Cer)。If this method throws an exception while copying, the destinationArray remains unchanged; therefore, ConstrainedCopy can be used within a constrained execution region (Cer).

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

適用於

另請參閱