C Sharp Programming/Introduction

To compile your first C# application, you will need a copy of a .NET Framework SDK installed on your PC.

There are two .NET frameworks available: Microsoft's and Mono's.

Microsoft .NET
For Windows, the .NET Framework SDK can be downloaded from Microsoft's .NET Framework Developer Center. If the default Windows directory (the directory where Windows or WinNT is installed) is, the .Net Framework SDK installation places the Visual C# .NET compiler (csc) in the

directory for version 1.0, the

directory for version 1.1, the

directory for version 2.0, the

directory for version 3.0, the

directory for version 3.5, or the

directory for version 4.0.

Mono
For Windows, Linux, or other Operating Systems, an installer can be downloaded from the Mono website. The Mono C# compiler is called mcs.

Linux
In Linux you can use the MonoDevelop IDE, and either download from their website at: MonoDevelop Downloads, or install via apt-get or your distro's installer.


 * Debian-based distros:

sudo apt-get install monodevelop


 * Arch Linux:

sudo pacman -S mono monodevelop

Windows
You can download MonoDevelop from their website at: Mono website. Click the Windows icon, and follow the installation instructions.

If you are working on Windows it is a good idea to add the path to the folders that contain cs.exe or mcs.exe to the Path environment variable so that you do not need to type the full path each time you want to compile.

For writing C#.NET code, there are plenty of editors that are available. It's entirely possible to write C#.NET programs with a simple text editor, but it should be noted that this requires you to compile the code yourself. Microsoft offers a wide range of code editing programs under the Visual Studio line that offer syntax highlighting as well as compiling and debugging capabilities. Currently C#.NET can be compiled in Visual Studio 2002 and 2003 (only supports the .NET Framework version 1.0 and 1.1) and Visual Studio 2005 (supports the .NET Framework 2.0 and earlier versions with some tweaking). Microsoft offers five Visual Studio editions, four of which are sold commercially. The Visual Studio C# Express Edition can be downloaded and used for free from Microsoft's website.

Hello, World!
The code below will demonstrate a C# program written in a simple text editor. Start by saving the following code to a text file called :

To compile, run the following from the command line:
 * For standard Microsoft installations of .NET 2.0,first cd into the directory with your source file then run
 * For Mono run.
 * For users of cscc, compile with.

Doing so will produce. The following command will run :
 * On Windows, use.
 * On Linux, use  or.

Alternatively, in Visual C# express, you could just hit F5 or the green play button to run the code. If you want to run without debugging, press CTRL-F5.

Running  will produce the following output: Hello, World! The program will then wait for you to strike 'enter' before returning to the command prompt.

Note that the example above includes the namespace via the  keyword. That inclusion allows direct references to any member of the namespace without specifying its fully qualified name.

The first call to the method of the  class uses a fully qualified reference.

The second call to that method shortens the reference to the class by taking advantage of the fact that the  namespace is included (with ).

C# is a fully object-oriented language. The following sections explain the syntax of the C# language as a beginner's course for programming in the language. Note that much of the power of the language comes from the classes provided with the .NET framework, which are not part of the C# language syntax per se.