G
Guest
I was viewing some strange behaviour in c# so I tried a
comparison in vb.net
A simple 1 line console application written in both c# and
vb.net (One line meaning one line in the main function).
Console.WriteLine(100L)
It appears vb.net declares a 8 byte long and stores the
value into it before calling the console.writeline method
which is what I would expect. However in c# it declares a
4 byte variable, and converts it to an 8 byte long,
wasting one extra instruction call.
Is this behaviour a bug in the framework or in fact vb.net
is more efficient at handling longs as apposed to c#.
The msil code for both programs is included below..
VB.net code ++++++++++++++++++++++++++++++++
..method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]
System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldc.i8 0x64
IL_0009: call void [mscorlib]
System.Console::WriteLine(int64)
IL_000e: ret
} // end of method Module1::Main
c# 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 9 (0x9)
.maxstack 1
IL_0000: ldc.i4.s 100
IL_0002: conv.i8
IL_0003: call void [mscorlib]
System.Console::WriteLine(int64)
IL_0008: ret
} // end of method Class1::Main
comparison in vb.net
A simple 1 line console application written in both c# and
vb.net (One line meaning one line in the main function).
Console.WriteLine(100L)
It appears vb.net declares a 8 byte long and stores the
value into it before calling the console.writeline method
which is what I would expect. However in c# it declares a
4 byte variable, and converts it to an 8 byte long,
wasting one extra instruction call.
Is this behaviour a bug in the framework or in fact vb.net
is more efficient at handling longs as apposed to c#.
The msil code for both programs is included below..
VB.net code ++++++++++++++++++++++++++++++++
..method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]
System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldc.i8 0x64
IL_0009: call void [mscorlib]
System.Console::WriteLine(int64)
IL_000e: ret
} // end of method Module1::Main
c# 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 9 (0x9)
.maxstack 1
IL_0000: ldc.i4.s 100
IL_0002: conv.i8
IL_0003: call void [mscorlib]
System.Console::WriteLine(int64)
IL_0008: ret
} // end of method Class1::Main