Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I make a lot of small programs in VB.Net. Of course, I have to declare variables. The way I declare "New" variables is
Public House As Building = New Building("live")

Is that the same as this?
Public House As New Building("live")

If not, what's faster, does it matter, what is safer?

Btw, I made the house idea while I was typing this.
Updated 22-Apr-12 13:29pm
Sergey Alexandrovich Kryukov 22-Apr-12 20:05pm    
Not so interesting question. Why not simply using the disassembler to figure it out? It would be faster than asking this question and waiting for the answer.
VJ Reddy 22-Apr-12 20:11pm    
Exactly. We thought same way.
I saw your comment after posting my answer.

Let us see
Sub Main
	Dim House As Building = New Building("live")
	Dim House2 As New Building("live")
End Sub

Public class Building
	Public sub New (Name as string)
	end sub
End class

The IL generated for the above code is
IL_0001:  ldstr       "live"
IL_0006:  newobj      UserQuery+Building..ctor
IL_000B:  stloc.0
IL_000C:  ldstr       "live"
IL_0011:  newobj      UserQuery+Building..ctor
IL_0016:  stloc.1

IL_0000:  nop
IL_0001:  ldarg.0
IL_0002:  call        System.Object..ctor
IL_0007:  nop
IL_0008:  nop
IL_0009:  ret

From the above it is clear that the IL is same in both cases. Hence, there is no difference in declaring in either way.
I used Dim instead of public as public is not allowed inside a method.
Share this answer
Sergey Alexandrovich Kryukov 22-Apr-12 20:15pm    
Thank you for taking the labor of running it. It was more or less apparent to me the two cases are the same, simply because I see no room for different IL. So, I answered, too, before I saw you answer -- please see.
Of course, my 5.
VJ Reddy 22-Apr-12 20:18pm    
Thank you, SA.
I used LINQPad, which has a IL tab in the output pan to quickly see the IL. I too thought there will not be any difference between the statements, but if the IL is seen it would be more clear. Hence, posted IL from LINQPad.
Sergey Alexandrovich Kryukov 22-Apr-12 21:15pm    
I appreciate the idea of LINQPad. As I almost never work with RDBMS, I never tried it. Do you mean using this the feature quoted below?
"Aside from being useful for writing queries, LINQPad can be used to interactively write C# code without having to compile it. This expands its use to a general "test workbench" where C# code can be quickly prototyped outside of Visual Studio."
Is it the same as "Code Scratchpad"?

What do you think about this tool? Is it non-obtrusive and light-weight? One concern is that it is proprietary software...
VJ Reddy 22-Apr-12 22:01pm    
You are correct. LINQPad is useful as Code Scratchpad to test code snippets quickly. Even though, the LINQPad is not an open-source product as given at, as per the license terms of the free version it can be useful for testing the code fragments.
In my experience, it is light-weight and non obtrusive.
Thank you.
Sergey Alexandrovich Kryukov 25-Apr-12 16:54pm    
Thank you very much for answering.
I don't think there can be any difference, but I don't know what was to ask about. I never install support of VB.NET projects with Visual Studio and lazy enough to test it with always available compiler via a command line, otherwise I would simply compile both cases, run compiled code through the disassembler ILDASM.EXE into IL and compared the output. Why even asking things like that?

Please see:[^].

Share this answer
VJ Reddy 22-Apr-12 20:28pm    
Good answer with an informative reference. 5!
Sergey Alexandrovich Kryukov 22-Apr-12 21:04pm    
Thank you, VJ.
Teamwork! :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900