The reference type that it
generates is derived from System.Array, and ultimately from System.Object. Therefore, you can
treat all arrays polymorphically through a reference to System.Array. That means that each
array, no matter what concrete type of array it is, implements all of the methods and properties
of System.Array.
You declare an array using parentheses following the array variable name. The following
example shows three ways to create an array of integers and print them to the console:
Imports System
Public Class EntryPoint
Shared Sub Main()
Dim array1() As Integer = New Integer(5) {}
Dim array2() As Integer = New Integer() {2, 4, 6, 8}
Dim array3() As Integer = {1, 3, 5, 7}
Dim i As Integer = 0
For i = 0 To array1.Length - 1
array1(i) = i * 2
Next
For Each item As Integer In array1
Console.WriteLine("array1: " + item.ToString)
Next
Console.WriteLine(vbCrLf)
197
C H A P T E R 1 0
For Each item As Integer In array2
Console.WriteLine("array2: " + item.ToString)
Next
Console.WriteLine(vbCrLf)
For Each item As Integer In array3
Console.WriteLine("array3: " + item.ToString)
Next
End Sub
End Class
Executing the preceding code will display the following results:
array1: 0
array1: 2
array1: 4
array1: 6
array1: 8
array1: 10
array2: 2
array2: 4
array2: 6
array2: 8
array3: 1
array3: 3
array3: 5
array3: 7
The longhand way to create an array instance and fill it with initial values is shown where
array1 is initialized.
Pages:
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331