views:

302

answers:

3

Duplicate

Whats the difference between declaring as new and as something new something i

Sad that I don't know this, but can anyone explain the difference between:

Dim X as New Y

and

Dim X as Y = New Y()
+9  A: 

The first just infers that the type is Y. The second specifies it. This allows you to write things like:

Dim X as IList(Of String) = New List(Of String)

That then limits the scope of the choice to use List(Of String) - which means that later on you might choose to use a different implementation of IList(Of String), and it'll still compile.

Of course, the shorter version does have the benefit of brevity - which can be important if you have very long type names (e.g. using nested generics). It's also required for anonymous types, where you can't express the type of X.

Jon Skeet
beat me to it; why am I surprised?
hometoast
+2  A: 

There is no difference. VB.NET offers:

Dim X As New Y

to be syntactically compatible with the old VB6 declaration syntax.

Justin Niessner
...argh. Beat by Skeet again.
Justin Niessner
+4  A: 

Jon Skeet has already provided the correct answer, but I thought I'd add a little extra...

Although it was not specifically your question, there is an important difference between

Dim X = New Y()

and

Dim X As Y = New Y()

The differences here actually depend on the version of VB.NET you are using.


VB.NET 7 & 8

Dim X = New Y()

This defines X as a variable of type object (System.Object), then creates an object of type Y and assigns it to X. Note that this only works when you've defined Option Strict Off, otherwise it won't even compile. It's really just a carry-over from the syntax of the days of VB6 and earlier, and is generally discouraged.

Dim X as Y = New Y()

This does exactly the same, except that X is now strongly-typed, in other words it is of type Y rather than object (from which all types inherit, including value types when they are boxed).

VB.NET 9 and subsequently

Dim X = New Y()

The difference here (for the newer version of VB.NET) is that type inference is taking effect here. Because you don't specify any type, but it is obvious to the compiler that New Y() returns an instance of type Y, it automatically infers that the type of X should be Y, which is a lot more specific than object. Type interference also works on more complex expressions.

Dim X as Y = New Y()

This is interepreted in precisely the same was as in VB.NET 7 & 8.


To conclude, you can effectively replace

Dim X As New Y()

with

Dim X = New Y()

in VB.NET 9 and the compiler will interpret the two lines exactly the same because of type inference. In earlier versions, however, the second line will be weakly typed (of type object), which is not advisable. Again, not a direct answer to the question, but I thought this might be helpful to point out.

Noldorin
Eh, I'm pretty sure your incorrect here, Dim x as new Y in VS 2005 gives you a strongly typed object. Pretty sure it was the same all the way back to the first version of .net (but don't have anything other than memory to go on, I've nothing older than 2005 installed here).
Binary Worrier
or am I missing something obvious . . .
Binary Worrier
@Binary Worrier: I'm pretty sure type inference was only introduced in VB.NET 9. I may still be wrong regarding the previous versions however, since it's been a long time since I programmed in VB.NET 8.
Noldorin
@Binary Worrier: I take it the downvote wasn't yours?
Noldorin
Right, so I misread the question here. My answer would have been correct for the question "Dim X = New Y vs. Dim X as Y = New Y()" (i.e. replacing `as` with `=`). I've edited the post to reflect all of that.
Noldorin
The down vote wasn't mine, and your comment "replacing `as` with `=`" makes perfect sense, we had both conspired to confuse the hell out of me :)
Binary Worrier
However the up vote is mine
Binary Worrier
@Noldorin: Your "Dim X = New Y() ... defines X as a variable of type object (System.Object)" is only correct if Option Strict is OFF. If Option Strict is ON, that won't compile in VB 7/8. And there's *NO* reason to write VB with Option Strict Off...;-). You may want to clarify.
Bob King
@Binary Worrier: Hehe, yeah. I think I confused myself pretty well at first, at least by misreading. Thanks for the up vote. :)@Bob King: Yeah, good point. I remembered that this syntax was carried over from VB6, but you do indeed need to turn Option Strict Off for it to work.
Noldorin
Thanks, removed downvote!
Bob King
Also VB 7 and 8 always support the Dim x as New Y() syntax.
Bob King
@Bob: Cheers. And yes, all versions of VB support that syntax. I don't think I've contradicted that, but I'll clarify the point too if you like.
Noldorin