IL Quiz...

[I'm going to try a Sahil thing]
In IL, what is the difference between the following statements:

[C#]
aClass Obj = new aClass();

[VB.Net example 1]
Dim Obj as aClass = new aClass()

[VB.Net example 2]
Dim Obj as new aClass()

Go ahead and comment... I'll post the answer later (although I bet this isn't that hard... I'm doing this for someone else's benefit)

[Everyone knows this... The code in IL is the same... BTW, Raymond, the C# compiler will also warn you that the Obj variable hasn't been used... this is I'm beginning to believe that the blue code is by far superior to the green code ]
Print | posted on Tuesday, April 19, 2005 11:59 AM

Feedback

# re: IL Quiz...

left by at 4/19/2005 2:37 PM Gravatar

All three will call newobj to create a new instance of the object and return and memory address to store on the stack.  However, the C# compiler is a bit more intelligent about the use of an object at compile time.  If it sees that you don't use the Obj variable, it actually won't get created and the reference to the object will just get popped back off the stack, meaning Obj will never exist.  The VB compiler doesn't do this and will actually assigned the memory address to the variable Obj, even though you may never use it.  Really, without seeing the context in which you wish to implement the line of code you have shown us, the MSIL will look different surrounding these statements.  But I think the answer you were looking for is "they all look identical as far as the newobj instruction in IL".

# re: IL Quiz...

left by at 4/19/2005 3:05 PM Gravatar

Ok lemme do another Sahil thing -

"So whats the answer man :-P"

# re: IL Quiz...

left by at 4/19/2005 5:11 PM Gravatar

Blue code is always more efficient thatn green code. Therefore, since you used green fonts for C#, it will be more efficient. (he he he).

# re: IL Quiz...

left by at 4/19/2005 6:02 PM Gravatar

It's all the same thing to the Framework.  But the C# code still gets paid more.

# re: IL Quiz...

left by at 4/19/2005 7:26 PM Gravatar

Doesn't "Dim Obj as new aClass()" have some sort of side effect?  Or has that been eliminated.  I think if you used that syntax in VB6, you could have unintended consequences - the object would be created whenever it was referenced.  So if you had:

Set Obj = Nothing

in some cleanup code, even if the Obj had already been deleted, it would be created again and then set to Nothing.

It is completely possible I am making all of this up.

Title  
Name
Email (never displayed)
Url
Comments   
Please add 3 and 1 and type the answer here: