November 27, 2014
Hot Topics:

C# and Intermediate Language (IL)

  • September 4, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

In the last two articles, we have seen about .NET and its related aspects. In this article, we will analyze the "Hello C#" program discussed in the previous article in detail. In the end, we will cover its Intermediate Language Code as well.

Detailed Analysis of the "Hello C#" Program

In this article, we will analyze the program, which we discussed in the previous article, in a more detailed manner.

The source code of the "Hello C#" program is reproduced again for your reference and for better understanding:

Listing 1

 1.  using System;
 2.    class Hello
 3.    {
 4.        public static void Main (string[] args)
 5.        {
 6.          Console.writeLine ("Hello C#");
 7.        }
 8.    }

Please note that line numbers are given for explanation (as seen in the following section) and are not the part of the source code.

Line-by-Line Analysis

Line Number Analysis
Line 1 It's called the namespace. Simply speaking, a namespace is a collection of .NET Classes similar to packages in Java.
Line 2 It is the class declaration, similar to Java and C++.
Line 3 This is the opening curly brace, which is required in all class declaration statements.
Line 4 This is the main method, which is required in all C# applications. It should be declared as public and static. The void keyword is not necessary, but you should use the return keyword to properly run the program. We will discuss the parameter inside the method later.
Line 5 This is the opening curly brace, which is required along with the Main() method.
Line 6 This code will print Hello C# onto the Console. Here, Console is the class belonging to the System namespace and writeLine is a static method belonging to the Console Class.
Line 7 It's the ending of the class declaration.
Line 8 It's the ending of the Main() method.

The Hello C# Program Without the void Keyword

Listing 2

 1.   using System;
 2.   class Hello
 3.   {
 4.      public static int Main (string[] args)
 5.      {
 6.         Console.writeLine ("Hello C#");
 7.         return;
 8.      }
 9.   }

Commenting the Code

You can comment out certain portions of the code. For example, if you want to give your name and e-mail address along with the source code, you can do so by commenting the entries. The C# compiler won't compile whatever code is inside these comments.

There are two kinds of comments in C#. They are single-line and multiline comments. Single-line comments can be coded within the "//" symbols, and multiline comments can be coded with the "/*....*/" symbols. See the code snippet given below for an example of each type of comment:

//This is a single-line comment


/* This is a
   Multiline comment */

A Look into the Inner Aspects

In the previous session, we examined the step-by-step process of creating the "Hello C#" program. In this session, we will analyze the Intermediate Language, also called IL, generated by the C# compiler.

What Is the Intermediate Language?

This is the language code generated by the C# compiler or any .NET-aware compiler. All .NET languages generate this code. This is the code that is executed during runtime.

You can view this MSIL code with the help of a utility called Intermediate Language Disassembler (ILDASM). This utility displays the application's information in a tree-like fashion. Because the contents of this file are read-only, a programmer or anybody accessing these files cannot make any modifications to the output generated by the source code.

To view the MSIL Code for the preceding Hello C# program, open the Hello.exe file from the ILDASM tool; it can be accessed via the Run command on the Start menu. You have to locate this tool manually. Normally, it will be in the Bin folder of the .NET SDK installation. Figure 1 shows an outline of this tool.

Figure 1

IL for the "Hello C#" Program

The Intermediate Language code generated by the C# compiler for the preceding "Hello C#" program is given below for your reference. Please note that only a portion of the code is displayed due to space constraints.

Listing 3

.class private auto ansi beforefieldinit Hello
       extends [mscorlib]System.Object
{
} // end of class Hello

Listing 4

method public hidebysig specialname rtspecialname
        instance void  .ctor() cil 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 Hello::.ctor

Listing 5

.method public hidebysig static void  Main(string[] args)
                                      cil managed
{
  .entrypoint
  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  ldstr      "Hello C#"
  IL_0005:  call       void [mscorlib]System.Console::
                                             WriteLine(string)
  IL_000a:  ret
} // end of method Hello::Main

Don't worry if you do not understand anything from the preceding stuff. It's not necessary that you understand this code. But you should know what this code looks like. Take a look at this code using the ILDASM utility for every C# and .NET program.

About the Author

Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials. He can be reached at anand@learnxpress.com.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel