: C# 2008 Programmer

Assemblies and the Microsoft Intermediate Language (MSIL)

Assemblies and the Microsoft Intermediate Language (MSIL)

In .NET, an application compiled into MSIL bytecode is stored in an assembly. The assembly is contained in one or more PE (portable executable) files and may end with an EXE or DLL extension.

Some of the information contained in an assembly includes:

?Manifest Information about the assembly, such as identification, name, version, and so on.

?Versioning The version number of an assembly.

?Metadata Information that describes the types and methods of the assembly.

Assemblies are discussed in more detail in Chapter 15.

To get a better idea of a MSIL file and its content, take a look at the following example, which has two console applications one written in C# and the other written in VB.NET.

The following C# code displays the "Hello, World" string in the console window:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorldCS {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}

Likewise, the following VB.NET code displays the "Hello, World" string in the console window:

Module Module1
Sub Main()
Console.WriteLine("Hello, World!")
Console.ReadLine()
End Sub
End Module

When both programs are compiled, the assembly for each program has an .exe extension. To view the content of each assembly, you can use the ildasm (MSIL Disassembler) tool.

Launch the ildasm tool from the Visual Studio 2008 Command Prompt window (Start?Programs?Microsoft Visual Studio 2008?Visual Studio Tools?Visual Studio 2008 Command Prompt).

The following command uses the ildasm tool to view the assemblies for the C# and VB.NET programs:

C:MSIL>ildasm HelloWorldCS.exe
C:MSIL>ildasm HelloWorldVB.exe

Figure 1-3 shows the content of the C# and VB.NET assemblies, respectively.


Figure 1-3

The Main method of the C# MSIL looks like this:

.method private hidebysig static void Main(string[] args) cil managed {
.entrypoint
// Code size19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr"Hello, World!"
IL_0006: callvoid [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: callstring [mscorlib]System.Console::ReadLine()
IL_0011: pop
IL_0012: ret
} // end of method Program::Main

The Main method of the VB.NET MSIL looks very similar to that of the C# program:

.method public static void Main() cil managed {
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size20 (0x14)
.maxstack 8
IL_0000: nop
IL_0001: ldstr"Hello, World!"
IL_0006: callvoid [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: callstring [mscorlib]System.Console::ReadLine()
IL_0011: pop
IL_0012: nop
IL_0013: ret
} // end of method Module1::Main

The important thing to note here is that regardless of the language you use to develop your .NET applications, all .NET applications are compiled to the MSIL bytecode as this example shows. This means that you can mix and match languages in a .NET project you can write a component in C# and use VB.NET to derive from it.


: 1.403. /Cache: 3 / 1