Writing your first C# Program - Hello World (.NET v1.1)
Add Comment<span class=wboxheado>Introduction</span><br> 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 <a class="wbox" target="_blank" href="http://www.mastercsharp.com/article.aspx?ArticleID=88&&TopicID=4">this</a> 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.<br> <br> 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.<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><span class="cmt">/* HelloWorld.cs - First C# Program Written by - Saurabh Nandu Compilation: csc HelloWorld.cs */</span> public class HelloWorld { public static void Main() { <span class="cmt">//Print Hello World</span> 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 <b>Save</b> 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 <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 <b>HelloWorld.cs</b> (although you can provide any name you want but it should have an extension .cs). and click <b>Save</b>. Once you have saved the source code, and if you make any further modifications, in notepad use the <i>Ctrl+S</i> keyboard short-cut to save your source code file.<br> </p> <p align="center"> <img border="0" src="../../img/helloworldfirst1.gif" width="563" height="455"><br> <b>Figure 1:</b> <i>Notepad Save As dialog </i><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>. Or go to <i>Start -> Run</i>, type <b>cmd</b> and press enter.<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. Refer to the Installing the .NET v1.1 SDK article for more information on setting paths.<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 align="center"> <img border="0" src="../../img/helloworldfirst2.gif" width="573" height="223"><br> <b>Figure 2:</b> <i>Compilation of C# source code </i> <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. <br> 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 align="center"> <img border="0" src="../../img/helloworldfirst3.gif" width="573" height="235"><br> <b>Figure 3:</b> <i>Executing Hello World application</i><p> Congratulations you have successfully compiled your first C# program! Its time for you to proceed further and learn the C# language.<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.4322 directory.<p><span class="wboxheado">Next Step</span><br> Read <a href="http://www.mastercsharp.com/article.aspx?ArticleID=90&&TopicID=4">this article</a> 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 <br> <br> <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.