In this session,
we will analyze the program, which we discussed in the previous article in a
more detailed manner.
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 and are not the part of the source code
Line-by-Line Analysis
Line 1: It’s called as the
namespace. Simply speaking, a namespace is a collection
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 about
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 System namespace and writeLine is a static method belonging to Console Class.
Line 7: It’s the ending of
the class declaration.
Line 8: It’s the ending of the Main() method.
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. }
You can comment out certain portions of the code.
For example, if you would like to give your name and email address along with
the source code, you can do so by commenting the entries. The C# compiler
won’t compile whatever code inside these comments. There are two kinds of
comments in C#. They are single line and multi line comments.
Single line comments can be coded within “//”, while multiline comments can
be coded with “/*…..*/” symbols. See the code snippet given below:
//This is single line comment
/* This is Multiline comment */
This is the language code generated by
the C# compiler or any .NET aware compiler. All .NET languages generate this
code. It is this code, which is executed during runtime.
the ILDASM tool and can be accessed via the run command of the start menu.
You have to locate this tool manually. Normally, it will be in the Bin folder of
.NET SDK installation. Figure 1 given below shows an outline of this tool.

Examining the IL code for Hello C# program
The Intermediate Language code generated by the C# compiler for the “Hello C#” program above is given below for your reference. Please note that only a portion of the code is displayed due to space constraints.
.class private auto ansi beforefieldinit Hello
extends [mscorlib]System.Object
{
} // end of class Hello
.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
.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 above stuffs. Understanding of this code is not necessary. But you should know how this code looks like. Take a look at this code using the ILDASM utility for every C# and .NET programs.
About
the Author
Anand
Narayanaswamy is the Founder/Editor of Learnxpress.com. He is a Microsoft Most
Valuable Professional (MVP), Author and Trainer. Anand can be reached via the
contact desk of this site.