Name
Array.Copy Method
Class
System.Array
Syntax
Array.Copy(sourceArray, destinationArray, length) Array.Copy(sourceArray, sourceIndex, destinationArray, _destinationIndex, length)
-
sourceArray Use: Required
Data Type: Any array
The array to be copied
-
sourceIndex Use: Required in second overloaded version
Data Type: Integer
The index in
sourceArrayat which copying begins-
destinationArray Use: Required
Data Type: Any array
The target array
-
destinationIndex Use: Required in second overloaded version
Data Type: Integer
The index in
destinationArraywhere the first element is to be copied-
length Use: Required
Data Type: 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 ...
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