在 VB.NET 中声明一个字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3927002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:04:47  来源:igfitidea点击:

Declaring a byte array in VB.NET

vb.netbytearraybyte

提问by Ender

When declaring a byte array, what is the difference between the following? Is there one, or are these just two different ways of going about the same thing?

声明一个字节数组时,下面有什么区别?有没有,或者这只是处理同一件事的两种不同方式?

Dim var1 As Byte()
Dim var2() As Byte

采纳答案by Hans Olsson

There's no difference.

没有区别。

Quotes from the spec(2003 spec, but same in the 2010 spec as can be downloaded here):

规范中的引用(2003 规范,但与 2010 规范相同,可在此处下载):

Array types are specified by adding a modifier to an existing type name.

数组类型是通过向现有类型名称添加修饰符来指定的。

A variable may also be declared to be of an array type by putting an array type modifier or an array initialization modifier on the variable name.

也可以通过在变量名称上放置数组类型修饰符或数组初始化修饰符来将变量声明为数组类型。

For clarity, it is not valid to have an array type modifier on both a variable name and a type name in the same declaration.

为清楚起见,在同一个声明中同时对变量名和类型名使用数组类型修饰符是无效的

And below is the sample from the spec that shows all the options:

以下是显示所有选项的规范示例:

Module Test
    Sub Main()
        Dim a1() As Integer    ' Declares 1-dimensional array of integers.
        Dim a2(,) As Integer   ' Declares 2-dimensional array of integers.
        Dim a3(,,) As Integer  ' Declares 3-dimensional array of integers.

        Dim a4 As Integer()    ' Declares 1-dimensional array of integers.
        Dim a5 As Integer(,)   ' Declares 2-dimensional array of integers.
        Dim a6 As Integer(,,)  ' Declares 3-dimensional array of integers.

        ' Declare 1-dimensional array of 2-dimensional arrays of integers 
        Dim a7()(,) As Integer
        ' Declare 2-dimensional array of 1-dimensional arrays of integers.
        Dim a8(,)() As Integer

        Dim a9() As Integer() ' Not allowed.
    End Sub
End Module

And as can be seen in the comments, a1 and a4 does the same thing.

从评论中可以看出, a1 和 a4 做同样的事情。

回答by DarinH

They're the same thing. You can verify by looking at the compiled code in reflector, or by writing that code in the IDE, then hovering your mouse over each.

它们是同一回事。您可以通过查看反射器中已编译的代码或在 IDE 中编写该代码,然后将鼠标悬停在每个代码上来进行验证。

They're reported as "var1() as byte" and "var2() as byte"

它们被报告为“var1() as byte”和“var2() as byte”

even though the first was declared with the alternate syntax.

即使第一个是用替代语法声明的。