C++ vs C# vs VB.NET
OK this post isn't really about this, so lemme back track a little and then you'll see where I am coming from with this.
I am the proud owner of Programming C# by Jesse Liberty (Great book on the whole). Whilst skimming through it this morning looking for something particular I noticed an interesting sidebar on page 73, Creating Objects, shown below for those who have not got the book on hand to flip to...
"VB programmers take note: while there is a performance penalty in using the VB keywords Dim and New on the same line, in C# this penalty is removed"
Huh? OK, I don't profess to be an expert in VB.NET but I was most interested in this. Never happy to simply take things at face value I thought I'd do a little digging and see what was going on under the hood, after all if there was some kind of performance issue in VB.NET then surely it would show up in IL? So off to my trusty friend ILDASM.EXE
This VB.NET code;
Sub Main()
Dim doc As New System.Xml.XmlDocument
End Sub
Produces the following IL;
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 9 (0x9)
.maxstack 1
.locals init ([0] class [System.Xml]System.Xml.XmlDocument x)
IL_0000: nop
IL_0001: newobj instance void [System.Xml]System.Xml.XmlDocument::.ctor()
IL_0006: stloc.0
IL_0007: nop
IL_0008: ret
} // end of method Module1::Main
And this C# code;
static void Main(string[] args)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
}
Produces the following IL code;
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 7 (0x7)
.maxstack 1
.locals init ([0] class [System.Xml]System.Xml.XmlDocument doc)
IL_0000: newobj instance void [System.Xml]System.Xml.XmlDocument::.ctor()
IL_0005: stloc.0
IL_0006: ret
} // end of method Class1::Main
So no rocket science here at all .... just creating a new instance of a XmlDocument to see why there would be a difference.
Any VB.NET expert wanna explain to me what Mr Liberty was referring to with his comments, I don't see it.All I see is a few "nop" statements in the VB.NET IL. Surely "nop" don't constitute a performance penalty, do they?
I went one step further and compared the IL for
Dim x as New System.Xml.XmlDocument
To
Dim x as System.Xml.XmlDocument = New System.Xml.XmlDocument
To
Dim x as System.Xml.XmlDocument
x = New System.Xml.XmlDocument
And in each of these three cases the IL produced was pretty much identical.
....
And now that we've gone through all of that i hear you screaming but what about C++. Quite observant, I have not mentioned it as yet. The only reason I included that in the title was because whilst I was digging around for the comparison between VB>NET and C# I came across an interesting post by Kate Gregory ( The American from Canada, :D private joke, guess you had to be there for that one .... ) entitled .NET Under the Hood: a Little ILDASM.
She does a similar thing but compares produced by C++, C# and VB.NET and comes to the conclusion that C# is in fact MORE like VB.NET than C++! At least in terms of what happens under the hood. So that puts to rest the argument that C# is better because it's more like C++ than VB. 