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类型必须destinationArrayArrayTypeMismatchException类型相同或派生, 否则将引发。The sourceArray type must be the same as or derived from the destinationArray type; otherwise, an ArrayTypeMismatchException is thrown. Copy不同ConstrainedCopy , 在执行任何操作之前, 验证数组类型的兼容性。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.

如果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. 如果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.

如果此方法在复制时引发异常, 则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.

适用于

另请参阅