[Mono-list] VB.NET and C# differences in compiling

Marco Parenzan marco.parenzan@libero.it
Mon, 30 Sep 2002 09:03:50 +0200


For example, see these two classes, one in VB and one in C#.

'VB class
public class MyClassVB

 public mMyVar as integer

 public sub New()
  mMyVar = 0
 end sub

 public function MyMethod(o as MyClassVB) as integer
  return mMyVar + o.mMyVar
 end function

end class

'C# class
public class MyClassCS
{
 public int mMyVar;

 MyClassCS()
 {
  mMyVar = 0;
 }

 public int MyMethod(MyClassCS o)
 {
  MyClassCS zObject = o;

  return mMyVar + zObject.mMyVar;
 }
}

Well. Compile and use ILDASM (note: I'm using .NET SDK sp2). THe method
"MyMethod" compile the same. The constructors compile quite different:

//VB MyClass Constructor IL Code
.method public instance int32  MyMethod(class MyClassVB o) cil managed
{
  // Code size       18 (0x12)
  .maxstack  2
  .locals init (int32 V_0)
  IL_0000:  ldarg.0
  IL_0001:  ldfld      int32 MyClassVB::mMyVar
  IL_0006:  ldarg.1
  IL_0007:  ldfld      int32 MyClassVB::mMyVar
  IL_000c:  add.ovf
  IL_000d:  stloc.0
  IL_000e:  br.s       IL_0010
  IL_0010:  ldloc.0
  IL_0011:  ret
} // end of method MyClassVB::MyMethod

//C# MyClass Constructor IL Code
.method public hidebysig instance int32  MyMethod(class MyClassCS o) cil
managed
{
  // Code size       20 (0x14)
  .maxstack  2
  .locals init (class MyClassCS V_0,
           int32 V_1)
  IL_0000:  ldarg.1
  IL_0001:  stloc.0
  IL_0002:  ldarg.0
  IL_0003:  ldfld      int32 MyClassCS::mMyVar
  IL_0008:  ldloc.0
  IL_0009:  ldfld      int32 MyClassCS::mMyVar
  IL_000e:  add
  IL_000f:  stloc.1
  IL_0010:  br.s       IL_0012
  IL_0012:  ldloc.1
  IL_0013:  ret
} // end of method MyClassCS::MyMethod

You see...there are minimal differences. VB.NET and C# manages stack space
in a different order and, in general case, in a different way.
I don't know the result (performance cost, I think) about these differences.
But I think that is not a problem. They are at version 1 and they are
already good.

This is a result of Microsoft's typical approach to allow different teams to
develop the same thing in different manners. The biggest (in .NET) is that
.NET Framework team and VS.NET team worked at the same time. So, in BCL, we
have different classes for (about) the same thing. For example:
System.XML.Serialization and System.Runtime.Serialization. They do the same
thing in different manner.

There are some books on IL and code generation:
- Inside Microsoft IL Assembler
(http://www.amazon.com/exec/obidos/tg/detail/-/0735615470/qid=1033368830/sr=
8-1/ref=sr_8_1/103-5164361-9848656?v=glance&n=507846) that is an Assemply
book
- CIL Programming
(http://www.amazon.com/exec/obidos/ASIN/1590590414/qid%3D1033368859/sr%3D11-
1/ref%3Dsr%5F11%5F1/103-5164361-9848656) that is a mix of Assemply book and
all things in .NET that have to do with IL (CodeGen, Reflection, Assembly
and compilers)
- Compiling for the .NET CLR
(http://www.amazon.com/exec/obidos/ASIN/0130622966/qid%3D1033368877/sr%3D11-
1/ref%3Dsr%5F11%5F1/103-5164361-9848656) that is a book on how to write a
compiler and, mainly, a code generator for .NET runtime.

Regards,
    Marco Parenzan

----- Original Message -----
From: "Gaurav Vaish" <gaurav.vaish@amsoft.net>
To: "Marco Parenzan" <marco.parenzan@libero.it>; <mono-list@ximian.com>
Sent: Monday, September 30, 2002 6:11 AM
Subject: Re: [Mono-list] Re: Mono-list digest, Vol 1 #524 - 21 msgs


> >
> > The better way to say this is: similar constructs implemented in C# and
> > VB.NET produce the same IL code.
> > If you declare a class in VB or in C#....it is compiled in a very
similar
> > way.
>
>     Thanks for that eye-opener (at least for me).
>     Do you have some pointers as to where I can find the subtle
differences
> in the C# and VB.Net - with respect to the IL code they produce.
>
> > You find the main differences, if you consider simple cases, in method
> > prologue and epilogue (some push or pop).
>
>     Can you be a little more elaborate on this. Seems interesting.
>
>
>
> Happy hacking,
> Gaurav
> http://mastergaurav.virtualave.net/iitk
> ----------------------------
>
>