-
If you really want to understand C # code, the best way to do that is by understanding the code generated by the C # compiler. This is the focus of this chapter and the following two chapters.
We will use a short C # program to uncover the mystery of IL and interpret the IL code generated by the compiler. In this way, we can "stone": first, we will reveal the mystery of IL, secondly, we will be more intuitive understanding of the C # programming language.
We will first show a. cs file and then write a program with Il via the C # compiler. Its output is the same as the CS file. The output will display the IL code. This will enhance our understanding of C # and IL. OK, no more nagging, this is the beginning of our adventure.
A.cs
class zzz
{
public static void Main()
{
System.Console.WriteLine("hi");
zzz.abc();
}
public static void abc()
{
System.Console.WriteLine("bye");
}
}
C:\IL>CSC A.cs
C:\il>ildasm/output=a.il A.exe
A.il
Microsoft (R). NET Framework IL disassembler. Version 1.0.2204.21
Copyright (C) Microsoft Corp. 1998-2000
Vtablefixup Directory:
No data.
. Subsystem 0x00000003
. corflags 0x00000001
. assembly extern mscorlib
{
. Originator = (D3 A4 AE)//. H.. 3
. hash = (F8 C9 1F 3F D7 AB AD E2 DF 1D E0
F2 9D 4F BC)//RD.. U.t? O.
. ver 1:0:2204:21
}
. Assembly A as "a"
{
---The following custom attribute is added automatically, does not uncomment-------
. custom instance void [Mscorlib]system.diagnostics.debuggableattribute::
. ctor (bool, bool) = (01 00 00 01 00 00)
. hash algorithm 0x00008004
. ver 0:0:0:0
}
. module A.exe
MVID: {3C938660-2A02-11D5-9089-9752D1D64E03}
. class private Auto ANSI ZZZ
extends [mscorlib]system.object
{
. method public hidebysig static void Main () il managed
{
. entrypoint
Code size (0x10)
. maxstack 8
Il_0000:ldstr "HI"
Il_0005:call void [Mscorlib]system.console::writeline (class System.String)
Il_000a:call void Zzz::abc ()
Il_000f:ret
}//End of method Zzz::main
. method public hidebysig static void ABC () IL managed
{
Code size (0XB)
. maxstack 8
Il_0000:ldstr "Bye"
Il_0005:call void [Mscorlib]system.console::writeline (class System.String)
Il_000a:ret
}//End of method zzz::abc
. method public Hidebysig specialname rtspecialname
instance void. ctor () Il managed
{
Code size 7 (0x7)
. maxstack 8
il_0000:ldarg.0
Il_0001:call instance void [Mscorlib]system.object::.ctor ()
Il_0006:ret
}//End of method Zzz::.ctor
}//End of class zzz
Disassembly COMPLETE ***********************
The above code is generated by the Il disassembler.