C# and Intermediate Language

By Anand Narayanaswamy

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
  IL_0006:  ret
} // end of method Hello::.ctor

Listing 5

.method public hidebysig static void  Main(string[] args)
                                      cil managed
  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  ldstr      "Hello C#"
  IL_0005:  call       void [mscorlib]System.Console::
  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.

About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in


  • I can't understand...

    Posted by Legacy on 12/31/2003 12:00am

    Originally posted by: Alex Max

    **** YOU !

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The Red Hat® Standard Operating Environment SOE helps you define, deploy, and maintain Red Hat Enterprise Linux® and third-party applications as an SOE. The SOE is fully aligned with your requirements as an effective and managed process, and fully integrated with your IT environment and processes. Benefits of an SOE: SOE is a specification for a tested, standard selection of computer hardware, software, and their configuration for use on computers within an organization. The modular nature of the Red …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds