Name
Array.Copy Method
Class
System.Array
Syntax
Array.Copy(sourceArray,destinationArray,length) Array.Copy(sourceArray,sourceIndex,destinationArray, _destinationIndex,length)
-
sourceArray(required; any array) The array to be copied
-
sourceIndex(required in second overloaded version; integer) The index in
sourceArrayat which copying begins-
destinationArray(required; any array) The target array
-
destinationIndex(required in second overloaded version; Integer) The index in
destinationArraywhere the first element is to be copied-
length(required; Integer) The number of elements to copy
Return Value
None
Description
Makes a copy of all or part of an array.
Since arrays are reference types, when we set one array variable equal to another, we are just assigning a new reference to the same array. For instance, consider the following code:
Dim a( ) As Integer = {1, 2, 3}
Dim b( ) As Integer
' Array assignment
b = a
' Change b
b(0) = 10
' Check a
MsgBox(a(0)) 'Displays 10The fact that changing b(0) also changes a(0) shows that a and b point to the same array.
Rules at a Glance
Using the first syntax, you can copy a range of values from the beginning of
sourceArrayto the beginning ofdestinationArray. Using the second syntax, you can copy a range of values from anywhere indestinationArrayto anywhere intargetArray.sourceArrayanddestinationArraymust have the same number of dimensions.lengthis the total number of elements to be copied. If sArr1 is a two- dimensional array, for example, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access