Master C# Logo banner
Welcome to - Master C#, the easy way... - by Saurabh Nandu


Writing your first C# Program - Hello World (.NET v1.1)

Add Comment

In this article you will learn to write your first C# program and compile it using the command-line C# compiler. I strongly recommend you read this article first to correctly install the .NET v1.1 SDK on your computer. The goal of this article is to get you comfortable with writing a C# program and compiling it. The code you type will be explained in later articles. So dont worry if you cant understand any code in this article.

As tradition goes the first program though is the Hello World program, which does nothing more but print a simple "Hello World" message on the console screen. Youll need a text editor (like Notepad) and the .NET v1.1 SDK installed on your computer to work with this example.

Let's begin!

Step 1: Start notepad from Start -> Program Files -> Accessories -> Notepad so that you can write the Hello World program. The program you write in C# is also called as source code.

Step 2: Write the Hello World program, you can either type the program shown below into notepad or just copy-paste it from this article.

 HelloWorld.cs - First C# Program
 Written by - Saurabh Nandu
 csc HelloWorld.cs

public class HelloWorld
  public static void Main()
    //Print Hello World
    System.Console.WriteLine("Hello World !");

Note 1: C# is a case-sensitive language hence in C# class HelloWorld, class helloWorld and class helloworld are treated differently, so be very careful while typing and type exactly as shown !!

Note 2: Indenting (leaving spaces before each line), is not mandatory, but its a good practice since it makes it easier to read indented code.

Step 3: Once you have finished typing your program you should Save the source code file. In fact after making any changes to your source code, you should always save the file. To save the file for the first time in notepad click on
File menu -> Save As. In the Save As dialog select the directory from the Save In dropdown where you want to save your files, I generally save my files to C:\csharp, and then in the File name textbox, enter the file name as HelloWorld.cs (although you can provide any name you want but it should have an extension .cs). and click Save. Once you have saved the source code, and if you make any further modifications, in notepad use the Ctrl+S keyboard short-cut to save your source code file.

Figure 1: Notepad Save As dialog

Note 3: C# source code files are stored with the extension .cs.

Note 4: In notepad's Save As dialog, File name textbox, always enter the file name in quotes (" ") so that the file is saved with the correct *.cs extension, else on some operating systems if you miss the quotes the file is saved with the extension *.txt (default for text files) due to which your files may not compile.

Step 4: Since you have finished writing the source code its time to compile it. Since we are using a command-line compiler that ships with the .NET SDK, start the command prompt from Start -> Program Files -> Accessories -> Command Prompt. Or go to Start -> Run, type cmd and press enter.

Note 5: If you have installed Visual Studio.NET then start the command prompt from Start -> Program Files -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools -> Visual Studio .NET Command Prompt. We use this prompt since it has the correct path settings for the C# compiler. Refer to the Installing the .NET v1.1 SDK article for more information on setting paths.

Now from the command prompt navigate to the directory where you have stored the source code file by issuing the following DOS commands.

cd\ - To navigate to the root of the drive
cd csharp - To navigate to the csharp directory.

Once you are in the csharp directory where you saved the source code file earlier, its time to run the C# Compiler csc.exe. Issue the following command to compile our HelloWorld.cs program:

csc HelloWorld.cs

You should see the output similar to that shown in figure below indicating the file was successfully compiled.

Figure 2: Compilation of C# source code

Step 5: If the compilation of the source code was successful then a new executable (Exe) file by the name HelloWorld.exe will be created in the directory you compiled the source code.
To execute the program simply type the name of the executable file at the command prompt and you will see the output of the program (Hello World !) as shown below.

Figure 3: Executing Hello World application

Congratulations you have successfully compiled your first C# program! Its time for you to proceed further and learn the C# language.

Points to Remember
1) C# code can be written in any text editor like notepad.
2) C# Source Code files are saved with the extension .cs.
3) C# is a case-sensitive language so you have to be careful while typing.
4) C# runs on the .NET Platform, hence you need to install the .NET SDK in order to compile C# programs.
5) The C# compiler is contained within the file csc.exe, which generally resides in the C:\windows\Microsoft.NET\Framework\v1.0.4322 directory.

Next Step
Read this article that explains the various part of code written in the Hello World program. It also explains the compilation and execution process on the .NET Platform

In this small article you learnt how to write and compile your first C# application. In the next few articles I will cover more in-dept on the C# Language fundamentals.


Add Comment