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 (.NET v1.1)
   
 

 

[Rate this Article]
Introduction
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
 Compilation:
 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

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 - 07 February 2005


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
4 keep it up!!! - Ashit on 8/21/2006 1:03:00 AM
Just Comments GREAT WORK. initially i was little nervous cause of the umpteen attempts i made to compile my first program using c# in .net Framework SDK by following steps WRITTEN IN OTHER C# SITES with little success. finally after searching so many sites i compiled my hello world program successfully in c# with the help of MASTERCSHARP tutorials. great tutorials with full details and figures. thanks a lot - guitsy on 9/8/2006 7:33:00 AM
5 through this i can learn all the C# languages if this site allows me to. - sadakat on 12/6/2006 10:49:00 PM
3 Very good and procedural - arunprasad on 12/28/2006 11:17:00 PM
5 Saya org baru masuk group ini,untuk berniaga dengan ikhlas ,,, - Zarinal Binti Idris on 1/16/2007 10:21:00 PM
5 It's a quite good job.It will definitely give a worthy help for the freshers - charuma on 2/16/2007 10:58:00 PM
4 The Procedure What You have Given is Excellent. But while Doing in that Procedure I am not able to Compile My first Program. Because The path, csc.exe is not set as the default path. We have to set the path in Environmental variables of Mycomputer Properties. - Deepthi on 3/1/2007 1:14:00 AM
Just Comments This didn't solve my problem, when i'm try to compile that hello.cs program i'm getting error hello.cs file din't find, - nitin on 4/12/2007 12:29:00 AM
5 This is absolutely brilliant!It is very explanatory and realy helps me to learn C# in detail!Just great!Keep it up!! - Sheniqua on 5/8/2007 8:12:00 AM
2 nothing - xxx on 6/17/2007 10:23:00 PM
5 thanq for ur information - srikanth on 6/23/2007 11:06:00 PM
5 Excellent - manoj on 7/24/2007 2:36:00 PM
4 It is good, and i'm novice to the subject - Mansoor on 8/14/2007 3:51:00 AM
5 Thanks for explaining every minute detail from the begining to to end. This is very helpful for Freshers. Thank you - Sanjay Kadam on 9/3/2007 2:32:00 AM
Just Comments - x on 9/3/2007 3:26:00 AM
5 x - x on 9/3/2007 3:27:00 AM
5 thank you very much. My prof asked me to make hello world program I didn't what she was talking about but after reading your source, it help me a lot thank you again - Armel Kone on 9/6/2007 9:24:00 AM
4 - Sush on 10/29/2007 10:50:00 PM
Just Comments Great job ! By the way, all you need to compile are: - csc.exe - cscomp.dll - cscompui.dll - msvcr70.dll - Philippe on 1/3/2008 10:24:00 AM
1 I want to learn more, more, and more like to become a master of c sharp - gsilverkey on 2/10/2008 5:36:00 AM
5 - divya on 4/10/2008 4:45:00 AM
3 its good for beginners who looks to compile the code in Command Prompt.! - mveshgan on 6/15/2008 11:29:00 PM
5 hey need help....just like e have netbeans for java...do we have anything such software for c#...if yes where do i get it from...thanks.. reply to me via email - idrish on 7/12/2008 2:42:00 AM
5 - ck on 9/29/2008 10:24:00 PM
Just Comments Thanx ..me too ..after searching so many sites .could make my first hellowworld from this guide - nabanita on 11/25/2008 2:48:00 AM
5 G8! - amey on 2/20/2009 9:55:00 PM
5 AM COPING WITH YA - Tolu Ademilua on 2/25/2009 10:38:00 AM
5 AMAAAAAAAAZING!!! - Malloli... on 3/14/2009 11:07:00 AM
5 hello saurabh.....its really very nice to understand the cmd. thanks for creating such tutorial.... please if could mail me list of commands which could be executed in C#. - Nutan on 6/16/2009 9:05:00 PM
1 Good way for understanding . - Hitesh kumar on 7/5/2009 6:20:00 AM
5 thanks for basic knowledge - prashant katiyar on 8/2/2009 11:39:00 AM
3 Great help for learning basics!!! - Priyanka on 8/29/2009 10:45:00 PM
Just Comments good......but i want to learn more..... - imran on 9/28/2009 10:05:00 PM
4 its really helpful to those who know nthng about c# - kattie on 10/7/2009 5:58:00 AM
Just Comments 1)How the path autometically change when v chenge drive..give code for it in .net window application with c#.. 2) i take user control.Make one panel and drop it on 1 form.now i want to move next record while button is oressed..give code for it in .net window application with c#.. 3)same as above but difference is i have to show data in list box.. give me code it's urgent within 1 day... Thanking U.. - vrayh on 11/1/2009 10:26:00 PM
5 ^-^ - wayne clyde paule on 1/12/2010 12:58:00 AM
4 great tutorial, thanks <a href="http://playfroggeronline.blogspot.com">play frogger online</a> - steve on 2/15/2010 4:46:00 PM
3 i am find out my solution from this site.so it is good site according to my opinion - Jayesh Patel on 3/11/2010 2:02:00 AM
5 super - dinesh on 3/11/2010 11:33:00 PM
2 nice, thanx for help - fiza on 4/21/2010 2:45:00 AM
2 nice, thanx for help - fiza on 4/21/2010 2:45:00 AM
5 Nice to understand how c# works basically :) - Mohsin Naveed on 4/30/2010 7:45:00 AM
5 poo - scarsey on 5/20/2010 7:07:00 AM
5 - Shalini on 5/25/2010 11:15:00 PM
5 cheptaru anni - cheptaru anni on 6/22/2010 9:16:00 PM
5 Excellent! - Paresh on 7/18/2010 9:36:00 AM
5 u r write very good & correct so rating excellent - gaurav gurjar on 7/29/2010 3:40:00 AM
5 Good Work. I like it......... - Neelam on 8/5/2010 9:49:00 PM

[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