- When C# (or any other .NET-friendly language) is compiled, the source code is transformed into an "Assembly"
- Assemblies are the .NET binaries, comprised of Metadata and the platform-independent Microsoft intermediate language (MSIL or CIL)
- MSIL is very similar to Java byte code
- The code remains as MSIL until a block of it is required by the Common Language Runtime (CLR)
- The assemblies themselves are also described using metadata. The description is termed a manifest
- The manifest holds:
- Information about the current version of the assembly
- Culture information (used for localizing string and image resources)
- A list of all externally referenced assemblies that are required for proper execution
- When more than one language has been compiled, there are multiple binaries, each of which is called a module
- Here is an example of a standard C# program:
using System;
//Here is a simple calculator that can do addition
namespace Calculator
{
public class CalcApp
{
public static void Main()
{
Calc c = new Calc();
int ans = c.Add(10,84);
Console.WriteLine("10 + 84 is {0}.", ans);
Console.ReadLine();
}
}
public class Calc
{
public int Add(int x, int y)
{ return x + y; }
}
}
- Here is the Add() method after the C# compiler has turned it into MSIL:
.method public hidebysig instance int32 Add(int32 x, int32y) cil managed
{
// Code size 8(0x8)
.maxstack 2
.locals init ([0] int 32 CS$00000003$0000000)
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: add
IL_0003: stloc.0
IL_0004: br.s IL_0006
IL_0006: ldloc.0
IL_0007: ret
} // end of method Calc::Add
- Can we make sense of this?
- For a comparison, look at the same method in VB .NET and its translation to MSIL:
Class Calc
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
End Class
- ... and its MSIL:
.method public instance int32 Add(int32 x, int32y) cil managed
{
// Code size 9(0x9)
.maxstack 2
.locals init ([0] int 32 Add)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: add.ovf
IL_0004: stloc.0
IL_0005: br.s IL_0007
IL_0007: ldloc.0
IL_0008: ret
} // end of method Calc::Add