.NET Type System, Part I - The ILDASM.EXE Tool
(Page 2 of 6 )
The .NET SDK includes a tool called ILDASM.EXE (Intermediate Language Disassembler) that you can use to inspect the generated .NET Assemblies and see the byte code instructions. For this section copy the following code, paste it into notepad, and save it as HelloILDASM.cs.
using System;
namespace HelloILDASM
{
public class Class1
{
static void Main()
{
Console.WriteLine("Hello ILDASM from C#");
}
}
}
Compile the application using the following command (note that I saved the file on drive C on my machine).
C:\csc HelloILDASM.cs
Use the following command to load the application with the ILDASM tool
C:\ildasm HelloILDASM.EXE
And this is exactly what you will get:

First of all, let's learn more about these icons. Examine the table below.
| Icon | Description |
 | This is a namespace in the loaded assembly; here we have the HelloILDASM namespace. |
 | This is a Reference Type symbol. We use this icon for Class1 (which is of course a Reference Type). |
 | This is a structure Type symbol; it's the same shape as the Reference Type, but a different color. |
 | This is an Interface symbol. In .NET there are some naming conventions, such as preceding the interface name with the I letter, as in IDisposable interface. |
 | This is an Enumeration type icon. |
 | This red arrow means more information can be obtained. |
 | This symbol represents an instance method. |
 | The S letter on the method symbol means that it's a static method. |
 | This symbol represents a field. |
 | This is a Property symbol. |
 | This is an Event symbol. |
 | This is the symbol for a static field or a constant. |
Understanding MSIL is beyond the scope of this article, but we will take a quick look at the MSIL Instructions. It's good for you to understand the MSIL code that the C# compiler generates from your source code, and in fact it will help you understand many concepts and give you the ability to develop better applications.
All the classes in the .NET Framework derive from a common base class which is called Object and lives in the System namespace. To illustrate this, double click on the More Information Icon of the Class1 class and the following class declaration window will be shown:

Actually this is no more than declaring a class (Class1) which is public and extending the class System.Object which lives in the assembly mscorlib. We didn't define that our Class1 would extend and inherit from System.Object; it's implicitly done for us by the C# Compiler. We will talk about this class soon. Close this window and double click on the .ctor(): void method and the next window will be shown:

This method declaration is the default constructor for our class Class1, and it does nothing more than call the System.Object constructor using the MSIL Instruction call. The method is marked as Managed Code using the cil managed keyword. Close this window and double click on the Main: void(). The next window will be shown:

As you know, every C# application has an entry point, and it's always the Main method. Here the .entrypoint defines the Main method as the application's entry point. Our C# source code was very simple; we just made a call to Console.WriteLine() method and passed the string "Hello ILDASM from C#". Let's see what the C# compiler has generated in the compiled assembly file.
IL_0000: ldstr "Hello ILDASM from C#"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
The MSIL code is a series of instructions that the CLR executes. The first instruction is ldstr, which loads the specified string constant onto the stack. The next instruction, call, calls the method System.Console.WriteLine(string). The method argument is taken from the stack and nothing is put back -- because the method returns void, which means that the method doesn't return a value.
I don't want to delve into a lot of details about the MSIL Instructions because it would fill a book. But I will show you the MSIL code in a lot of articles until you get used to it. It will help you understand the code that gets generated under the hood. It's not that difficult and you will not need to write .NET Applications using IL Assembler; in most cases you will be using C#.
Next: System.Object Class >>
More .NET Articles
More By Michael Youssef