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

Definition

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)

Parameters

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.

Exceptions

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.

Remarks

sourceArraydestinationArray 參數的維度數目必須相同。The sourceArray and destinationArray parameters must have the same number of dimensions. sourceArray 類型必須與 destinationArray 類型相同或衍生自。否則,會擲回 ArrayTypeMismatchExceptionThe 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 重迭,則此方法的行為就像是在覆寫 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. 如果 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.

如果這個方法在複製時擲回例外狀況,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.

Applies to

See also