This chapter and the next couple of them will focus on and elicit a simple belief of ours,
that if you really want to understand C# code in earnest, then the best way of doing so is
by understanding the IL code generated by the C# compiler.
So, we shall raise the curtains with a small C# program and then explain the IL code
generated by the compiler. In doing so, we will be able to kill two birds with one stone:
Firstly, we will be able to unravel(解开) the mysteries of IL and secondly, we will obtain a more
intuitive understanding of the C# programming language.
We will first show you a .cs file and then a program written in IL by the C# compiler, whose
output will be the same as that of the .cs file. The output will be displayed of the IL code.
This will enhance our understanding of not only C# but also IL. So, without much ado, lets
take the plunge.
The above code is generated by the il disassembler
After executing ildasm on the exe file, we studied the IL code generated by the program.
Subsequently, we eliminated parts of the code that did not ameliorate our understanding of
IL. This consisted of some comments, directives, functions etc. The remaining IL code
presented is as close to the original as possible.
The advantage of this technique of mastering IL by studying the IL code itself is that, we
are learning from the master, i.e. the C# compiler, on how to write decent IL code. We
cannot find a better authority than the C# compiler to enlighten us about IL.
The rules for creating a static function abc remain the same as any other function such as
Main or vijay. As abc is a static function, we have to use the static modifier in the .method
directive.
When we want to call a function, the following information has to be provided in the order
given below:
? the return data type.
? the class name.
? the function name to be called.
? the data types of the parameters.
The same rules also apply when we call the .ctor function from the base class. It is
mandatory to write the name of the class before the name of the function. In IL, no
assumptions are made about the name of the class. The name defaults to the class we are
in while calling the function.
Thus, the above program first displays "hi" using the WriteLine function and then calls the
static function abc. This function too uses the WriteLine function to display "bye".
Static constructors are always called before any other code is executed. In C#, a static
constructor is merely a function with the same name as a class. In IL, the name of the
function changes to .cctor. Thus, you may have observed that in the earlier example, we
got a free function called ctor.
Whenever we have a class with no constructors, a free constructor with no parameters is
created. This free constructor is given the name .ctor. This knowledge should enhance our
ability as C# programmers, as we are now in a better position to comprehend as to what
goes on below the hood.
The static function gets called first and the function with the entrypoint directive gets
called thereafter.