Declaring a byte array in VB.NET

后端 未结 2 1098
天命终不由人
天命终不由人 2021-01-11 16:26

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?



        
相关标签:
2条回答
  • 2021-01-11 16:38

    There's no difference.

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

    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.

    0 讨论(0)
  • 2021-01-11 16:39

    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.

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

    even though the first was declared with the alternate syntax.

    0 讨论(0)
提交回复
热议问题