In our VB.net codebase I occasionally see Dim x as new Y()
, where Y is a class.
What is this code doing and how does it differ to the more common Dim x as Y = new Y()"
?
Thanks.
They are exactly the same; the first is just a shortcut:
http://msdn.microsoft.com/en-us/library/Aa903373
As mentioned in @Gendolkari's answer, the code is exactly the same. But I dispute that Dim x As Y = new Y()
is more common. I've seen a lot of code from a lot of different places, and Dim x As New Y()
is a very commonly used shortcut.
However, it might be more common at your work place. If that's the case, I have to come down against using the shortcut. Coding standards are important. If used correctly, even simple deviations like this are valuable because they set up easy to spot red flags that the code may need deeper inspection for other flaws.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With