If I look at the IL that is created in Linqpad for the two following code snippets, I wonder what happens here.
In c#
int i = 42;
results in the following IL code
IL_0000: ret
whereas in VB
Dim i As Integer = 42
it is
IL_0000: ldc.i4.s 2A
IL_0002: stloc.0
Apparently, the c# compiler understands the the value is never used and thus simply returns nothing. In VB.NET the actual code is translated.
Is that due to differences in compiler optimization or is there anything else at work?
Update: Just to clarify this – I just enter this one line into LinqPad and look at the IL it creates (most definitely by running the respective compiler). There is no program.
Taking away the linqpad question, I ran
vbcandcscwith/optimize+ /debug-on these programs:and
and got these CIL results from ILDASM:
For the VB:
For the C#:
So, yes, at least in this respect
cscis ‘smarter’ thanvbc. But I bet the JITter would remove any difference at execution time.edit
I checked, and actually the executed native code is different, at least on my system. I put in
Console.ReadLine()calls in both to give me a chance to attach a debugger, and I got these disassemblies:From the VB:
From the C#:
Now, my assembley is pretty much non-existent, but even I can see that
in the from-VB refers to a constant value of hex
2A, which is 42 decimal. So there you go, it does execute more instructions in the end.