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