Learn how to program your first C# application

After understanding the fundamentals of .NET and its structure, let’s now move on to look at a classic Hello C# Program. As I mentioned in the previous tutorial, you can use any text editor according to your convenience.

Listing 1 gives the coding for this simple "Hello C#" program:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System ;
class Hello {
public static void Main () {
Console.writeLine (&quot;Hello C#&quot;); 


What to do Next?

(1) Save the following file as Hello.cs. cs is an extension to indicate C-sharp like .java for a Java Source File. You have to supply this extension while saving your file, otherwise the code will not compile correctly. The saved file will be of .cs.txt extension.

(2) Compile the above Code by giving the following Command at the Command Prompt csc Hello.cs

(3) If there are compile errors then you will be prompted accordingly, otherwise you will be getting a command prompt with the copyright information. Keep in mind that the compiler generates MSIL Code.

Your next job is to execute the program to view the final Output. For that purpose, you have to simply give Hello at the command Prompt. If everything goes on well, a message Hello C# will be printed on the Screen.

You can view the MSIL Code generated by any .NET Language with the help of an Utility called Intermediate Language Disassembler or shortly ILDASM. This utility will display the application’s information in a tree like fashion. Since the contents of this file is read only, a Programmer or anybody accessing these files cannot make any modifications to the output generated by the Source Code.

READ  What are Identifiers and Keywords?

To view the MSIL Code for the above Hello C# Program, open Hello.exe file from the ILDASM tool. In Windows 98, ME & NT this tool can be accessed via start | programs | Microsoft .NET Framework SDK | Tools – IL Disassembler

Detailed Review of the above Program

I am giving below the process occurring to the Source code, during the compilation and execution stages once again for your reference.

Compilation Stage

C# Compiler produces MSIL as output during compilation. It is itself a complete language and it is more or less similar to Assembly language. MSIL stands for Microsoft Intermediate Language. The same thing happens to any application written in any CLR Compliant Language like Visual Basic .NET, Visual C++ .NET etc.

Execution Stage

The MSIL is then compiled into native CPU instructions by using JIT (Just in Time) Compiler at the time of the program execution. The Source Code is recompiled and executed only after making any changes to it.

Now let us analyze the Hello C# Program as a whole. Please note that the line numbers are given for clarification and explanation and is not a part of the Source Code:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: using System ;
  2: class Hello   {
  3: public static void Main () {
  4: Console.writeLine (&quot;Hello C#&quot;)
  5: } 
  6: }

Line Number wise Analysis

Line 1: It is called as 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 & C++

READ  Learn how to work with Java Database Connectivity

Line 3: This is the main method which is required in all C# applications.

Line 4: This code will print Hello C# onto the console. Here console is the class belonging to System namespace and writeLine is the static method belonging to Console Class.

Line 5: The opening and closing curly races are similar to that of C++/Java.

Leave a Reply

Your email address will not be published.