在VB.NET中声明数组的不同方法。

时间:2021-06-15 21:15:04

In VB.NET, is there any difference between the following ways of declaring arrays?

在VB。NET,下面声明数组的方式有什么不同吗?

- Dim cargoWeights(10) as Double

- cargoWeights = New Double(10) {}

' These are two independent statements. They are not supposed to execute one after the other.

这是两个独立的声明。他们不应该一个接一个地执行。

As far as I know, the first one just declares an array variable that holds the value 'Nothing' until some array object is assigned to it. In other words, it is not initialized yet.

据我所知,第一个只是声明一个数组变量,该变量保存值“Nothing”,直到某个数组对象被分配给它为止。换句话说,它还没有初始化。

But what about the second statement? Does the "=" sign means the variable is already being initialized and won't hold 'Nothing'? Is it going to point to an one-dimensional array of eleven default Double values (0.0)?

但是第二种说法呢?“=”符号是否表示变量已经被初始化,并且不会保存“无”?它会指向11个默认值(0.0)的一维数组吗?

EDIT:

编辑:

According to MSDN website:

根据MSDN网站:

The following example declares an array variable that does not initially point to any array.

下面的示例声明一个数组变量,它最初并不指向任何数组。

Dim twoDimStrings( , ) As String

将两个dimstrings(,)调成String

(...) the variable twoDimStrings has the value Nothing.

(…)变量twoDimStrings没有任何值。

Source: http://msdn.microsoft.com/en-us/library/18e9wyy0(v=vs.80).aspx

来源:http://msdn.microsoft.com/en-us/library/18e9wyy0(v = vs.80). aspx

1 个解决方案

#1


4  

Both Dim cargoWeights(10) as Double and cargoWeights = New Double(10) {} will actually initialize an array of doubles with each items set to default type value, which in this case, 0.0. (Or Nothing if String data type)

Dim cargoWeights(10)均为Double, cargoWeights = New Double(10){}将实际初始化一个Double数组,每个条目设置为默认类型值,在本例中为0.0。(如果是字符串数据类型,则没有)

The difference between the two syntax is that, the 2nd one you can init the value of each items in the array to different from default value, like:

这两种语法的不同之处在于,第二个语法可以将数组中每个条目的值初始化为与默认值不同的值,比如:

cargoWeights = New Double(10) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

To declare uninitialized array, use Dim cargoWeights() As Double or cargoWeights = New Double() {}.

要声明未初始化的数组,请使用Dim cargoWeights()作为Double或cargoWeights = New Double(){}。

#1


4  

Both Dim cargoWeights(10) as Double and cargoWeights = New Double(10) {} will actually initialize an array of doubles with each items set to default type value, which in this case, 0.0. (Or Nothing if String data type)

Dim cargoWeights(10)均为Double, cargoWeights = New Double(10){}将实际初始化一个Double数组,每个条目设置为默认类型值,在本例中为0.0。(如果是字符串数据类型,则没有)

The difference between the two syntax is that, the 2nd one you can init the value of each items in the array to different from default value, like:

这两种语法的不同之处在于,第二个语法可以将数组中每个条目的值初始化为与默认值不同的值,比如:

cargoWeights = New Double(10) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

To declare uninitialized array, use Dim cargoWeights() As Double or cargoWeights = New Double() {}.

要声明未初始化的数组,请使用Dim cargoWeights()作为Double或cargoWeights = New Double(){}。