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 sourceArray at 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 destinationArray where 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 10

The 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 sourceArray to the beginning of destinationArray. Using the second syntax, you can copy a range of values from anywhere in destinationArray to anywhere in targetArray.

  • sourceArray and destinationArray must have the same number of dimensions.

  • length is the total number of elements ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.