Writing your first C# Program - Hello World
Add Comment<p><b>Note:</b> An updated version of this article for the .NET Framework v1.1 is available <a href="http://www.mastercsharp.com/article.aspx?ArticleID=89&&TopicID=4">here</a>.</p> <span class=wboxheado>Introduction</span><br> New to C# ?? Haven't got Visual Studio.NET ??<br> 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 <a class="wbox" target="_blank" href="article.aspx?ArticleID=17&&TopicID=10"> this</a> article first to correctly install the .NET SDK on your computer.<p>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.</p> <p><span class=wboxheado>Let's begin!</span></p> <p><b>Step 1:</b> Start notepad from <i>Start -> Program Files -> Accessories -> Notepad</i> so that you can write the Hello World program. The program you write in C# is also called as source code. </p> <p><b>Step 2:</b> Write the Hello World program, you can either type the program shown below into notepad or just copy-paste it from this article.</p> <table cellpadding="1" cellspacing="2" width="100%" class="code"> <tr> <td width="100%"><pre>/* 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 !" ); } } }</pre></td> </tr> </table> <p><b>Note 1:</b> <i>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 !!</i></p> <p><b>Note 2:</b> <i>Indenting (leaving spaces before each line), is not mandatory, but its a good practice since it makes it easier to read indented code.</i></p> <p><b>Step 3: </b>Once you have finished typing your program you should <i>Save</i> 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 <br> <i>File menu -> Save As</i>. In the <b>Save As</b> dialog select the directory from the <b>Save In </b>dropdown where you want to save your files, I generally save my files to <i>C:\csharp</i>, and then in the <b>File name</b> 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 <i>Ctrl+S</i> keybord short-cut to save your source code. </p> <p> <img border="0" src="../../img/helloworldfirst-1.gif" width="563" height="455"><p> <b>Note 3:</b> <i>C# source code files are stored with the extension .cs.</i><p> <b>Note 4:</b> <i>In notepad's <b>Save As</b> dialog, <b>File name</b> 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.</i><p> <b>Step 4:</b> 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 <i>Start -> Program Files -> Accessories -> Command Prompt</i>.<p> <b>Note 5:</b> If you have installed Visual Studio.NET then start the command prompt from <i>Start -> Program Files -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools -> Visual Studio .NET Command Prompt</i>. We use this prompt since it has the correct path settings for the C# compiler.<p> Now from the command prompt navigate to the directory where you have stored the source code file by issuing the following DOS commands.<br> <br> <b>cd\</b> - To navigate to the root of the drive<br> <b>cd csharp</b> - To navigate to the csharp directory.<br> <br> Once you are in the <i>csharp</i> directory where you saved the source code file earlier, its time to run the C# Compiler <b>csc.exe</b>. Issue the following command to compile our <i>HelloWorld.cs</i> program:<br> <br> <b>csc HelloWorld.cs </b> <br> <br> You should see the output similar to that shown in figure below indicating the file was successfully compiled.<p> <img border="0" src="../../img/helloworldfirst-2.gif" width="541" height="223"> <p> <b>Step 5:</b> If the compilation of the source code was successful then a new executable (Exe) file by the name <b>HelloWorld.exe</b> 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.<p> <img border="0" src="../../img/helloworldfirst-3.gif" width="533" height="235"><p> Congratulations you have successfully compiled your first C# program!<p> <span class=wboxheado>Points to Remember</span><br> 1) C# code can be written in any text editor like notepad.<br> 2) C# Source Code files are saved with the extension .cs.<br> 3) C# is a case-sensitive language so you have to be careful while typing.<br> 4) C# runs on the .NET Platform, hence you need to install the .NET SDK in order to compile C# programs.<br> 5) The C# compiler is contained within the file csc.exe, which generally resides in the C:\windows\Microsoft.NET\Framework\v1.0.3705directory.<p> <span class=wboxheado>Conclusion</span><br> 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.