What is the difference between Dim v As String() and Dim v() As String?

This may sound trivial, but what is the difference between

Dim v As String()

and

Dim v() As String

in VB.NET?


No difference. From the VB.NET Language Specification on Arrays:

Array types are specified by adding a modifier to an existing type name. The modifier consists of a left parenthesis, a set of zero or more commas, and a right parenthesis.

...

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. In that case, the array element type is the type given in the declaration, and the array dimensions are determined by the variable name modifier. 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.


Originally, in Basic, you had to define arrays, but not variables. And the types of variables were defined by a suffix character: A$ was a string, while A% was an integer and A# was double precision. (and all three were distinct and could be used at the same time) (For single-precision, you could use A!, but that was the default if you just used A)

Eventually, programmers came to realize that those were incredibly bad design choices.

To rectify this, Microsoft added "Option Explicit" which required you to predefine every variable. To lessen the effect on the language, they hijack the "DIM" command, which was used to define arrays, to define scalar variables as well.

So originally:

 DIM A(50)    ' define 51-element single-precision array

Then

 DIM A(50)    ' define 51-element single-precision array
 DIM A$       ' define a string

Then to get rid of the suffixes, they added the "As {type} syntax"

 DIM A(50)    ' define 51-element single-precision array
 DIM B as String 
 DIM C(50) as String ' define 51-element string array.

Then they made array size variable.

 DIM A()    ' define single-precision array
 DIM B as String 
 DIM C() as String ' define string array.

This left a conflict in definition style, so they allowed both:

 DIM A()    ' define single-precision array
 DIM B as String 
 DIM C() as String ' define string array.
 DIM D as String() ' define string array.