This documentation is archived and is not being maintained.

Array.ConstrainedCopy Method

Note: This method is new in the .NET Framework version 2.0.

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.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Public Shared Sub ConstrainedCopy ( _
	sourceArray As Array, _
	sourceIndex As Integer, _
	destinationArray As Array, _
	destinationIndex As Integer, _
	length As Integer _
Dim sourceArray As Array
Dim sourceIndex As Integer
Dim destinationArray As Array
Dim destinationIndex As Integer
Dim length As Integer

Array.ConstrainedCopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length)
public static void ConstrainedCopy (
	Array sourceArray, 
	int sourceIndex, 
	Array destinationArray, 
	int destinationIndex, 
	int length
public static function ConstrainedCopy (
	sourceArray : Array, 
	sourceIndex : int, 
	destinationArray : Array, 
	destinationIndex : int, 
	length : int



The Array that contains the data to copy.


A 32-bit integer that represents the index in the sourceArray at which copying begins.


The Array that receives the data.


A 32-bit integer that represents the index in the destinationArray at which storing begins.


A 32-bit integer that represents the number of elements to copy.

Exception typeCondition


sourceArray is a null reference (Nothing in Visual Basic).


destinationArray is a null reference (Nothing in Visual Basic).


sourceArray and destinationArray have different ranks.


The sourceArray type is neither the same as nor derived from the destinationArray type.


At least one element in sourceArray cannot be cast to the type of destinationArray.


sourceIndex is less than the lower bound of the first dimension of sourceArray.


destinationIndex is less than the lower bound of the first dimension of destinationArray.


length is less than zero.


length is greater than the number of elements from sourceIndex to the end of sourceArray.


length is greater than the number of elements from destinationIndex to the end of destinationArray.

The sourceArray and destinationArray parameters must have the same number of dimensions. The sourceArray type must be the same as or derived from the destinationArray type; otherwise, an ArrayTypeMismatchException is thrown. 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). 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.

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.

This method is equivalent to the standard C/C++ function memmove, not memcpy.

The arrays can be reference-type arrays or value-type arrays. If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. 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. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

If this method throws an exception while copying, the destinationArray remains unchanged; therefore, ConstrainedCopy can be used within a constrained execution region (CER).

This method is an O(n) operation, where n is length.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0