www.MasterCsharp.com Logo  AksTech Ad
 Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 

 

Writing your first C# Program - Hello World
   
 

 

[Rate this Article]

Note: An updated version of this article for the .NET Framework v1.1 is available here.

Introduction
New to C# ?? Haven't got Visual Studio.NET ??
In this small article I will take you through writing your first C# program, and compiling it using the command-line C# compiler. I strongly recommend you read this article first to correctly install the .NET SDK on your computer.

As tradition goes the first program you write is the Hello World program, which does nothing more but print a simple "Hello World" message on the console screen. You need a text editor (like Notepad) and the .NET 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
 Compilation:
 csc HelloWorld.cs
*/

namespace MasterCSharp.Samples
{
  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 it. In fact after making any changes to your source code you should always save the program. To save the file for the first time in notepad go to
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 keybord short-cut to save your source code. 

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.

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.

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.

 

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. Now 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.

Congratulations you have successfully compiled your first C# program!

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.3705directory.

Conclusion
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.

 

  
Saurabh Nandu - 16 June 2002


Your Ratings / Comments
     
 

[Go to Top]

How many cups of coffee is this article worth??

Rating (Bad)-(Excellent)

Your Name
Your E-mail  
Your Message (Optional)

Viewer Ratings/Comments
Rating Description
3 - nguyen phuc hai on 10/8/2002 10:15:00 PM
5 - Kevin Hu on 11/8/2002 10:17:00 AM
4 - test on 3/20/2003 5:37:00 PM
1 hi - mili on 4/3/2003 2:08:00 AM
2 nice one! - Danko on 4/10/2003 3:04:00 AM
5 - Sabelo on 7/7/2003 7:02:00 AM
5 - t6ey on 10/15/2003 6:10:00 PM
5 this is the best - manjula on 1/8/2004 9:52:00 PM
4 - eeww on 3/7/2004 1:23:00 AM
1 bad,bad................ - ddggdsdds on 3/26/2004 7:30:00 AM
1 sdfsdfdfdsfsdf - `12`12 on 4/24/2004 7:44:00 AM
5 fghgfh - df on 4/24/2004 7:45:00 AM
5 Good - Sam on 7/22/2004 2:43:00 AM
3 - terry warren on 1/22/2005 6:31:00 PM
4 this was good - Abhishek on 11/14/2005 2:18:00 AM
5 This site has helped me a lot to start off with the basics. Since I am still an amateur, I dont know much about the language and this website has helped me clear all my doubts. Thanx a lot...... - Pradyumna on 8/18/2006 2:42:00 AM
5 thank u for providing useful information. - madhu sudhan rao on 9/1/2006 12:52:00 AM
5 - komal on 3/10/2007 12:23:00 PM
5 - raman on 3/15/2007 11:19:00 PM
3 Very nicely put in . good approach . Thx for posting - Srinivas Pemmaraju on 3/31/2007 7:05:00 PM
5 thnaks aa lot ... this was a gr8 starter - sita on 4/9/2007 1:46:00 AM
3 i have read the above page and tried on my own to execute the program it works.... thanks - sathish kumar P on 4/9/2007 4:43:00 AM
5 Better Than Best - Vinayak on 5/15/2007 11:43:00 PM
Just Comments its good for beginners - haripriya on 5/24/2007 10:03:00 PM
5 It good 4 begineer........ - Mukesh Ranjan on 6/4/2007 6:33:00 AM
5 - Darryl Merkley on 10/3/2007 11:29:00 AM
5 thank you it was extremely helpful - matthew ortyl on 1/12/2008 6:52:00 PM
5 i wish i found this website before .. neways .. how do i get to more depth ?? .. like i need to know how to code a menu of cars in notepad ... any example of such a thing should help .. thanks - nahid on 8/11/2008 3:37:00 AM
5 This article is very usefull - vengat on 2/18/2010 1:51:00 AM
5 This article was nice to learn the C# program for beginners. I want to know more depth with some examples. Can you please provide some more examples? - Vengatesan on 2/18/2010 1:55:00 AM
1 thnax a billion , i was sacred initially but you gave me a very nice starting., - hanumant sharma on 4/28/2010 11:15:00 AM

[Go to Top]


 
 
  Copyright © 2002 - 2004 MasterCSharp.com. All rights are reserved.

  Presenting MasterCSharp.com in association with AksTech Solutions - .NET Solutions Development and Consulting. 

  Best Viewed in IE 4.0+ and 800x600 Resolution