Visual Basic .NET数组定义

示例

Dim array(9) As Integer ' Defines an array variable with 10 Integer elements (0-9).

Dim array = New Integer(10) {} ' Defines an array variable with 11 Integer elements (0-10)
                               'using New.

Dim array As Integer() = {1, 2, 3, 4} ' Defines an Integer array variable and populate it
                                      'using an array literal. Populates the array with
                                      '4 elements.

ReDim Preserve array(10) ' Redefines the size of an existing array variable preserving any
                         'existing values in the array. The array will now have 11 Integer
                         'elements (0-10).

ReDim array(10) ' Redefines the size of an existing array variable discarding any
                'existing values in the array. The array will now have 11 Integer
                'elements (0-10).

从零开始

中的所有数组VB.NET都是从零开始的。换句话说,VB.NET数组中第一项的索引(下限)始终为0。默认情况下,较旧的VB版本(例如VB6和VBA)是基于一个的,但是它们提供了一种覆盖默认范围的方法。在早期版本的VB中,可以明确说明下限和上限(例如Dim array(5 To 10),在VB.NET中,为了保持与其他.NET语言的兼容性,已删除了灵活性,并且0始终强制执行下限。) ,To仍然可以在VB.NET中使用该语法,这可以使范围更加明确,例如,以下示例均与上面列出的示例等效:

Dim array(0 To 9) As Integer

Dim array = New Integer(0 To 10) {} 

ReDim Preserve array(0 To 10)

ReDim array(0 To 10)

嵌套数组声明

Dim myArray = {{1, 2}, {3, 4}}