C# 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
[edit | edit source]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 C:\WINDOWS
, the .Net Framework SDK installation places the Visual C# .NET compiler (csc) in the
C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705
directory for version 1.0, the
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
directory for version 1.1, the
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
directory for version 2.0, the
C:\WINDOWS\Microsoft.NET\Framework\v3.0
directory for version 3.0, the
C:\WINDOWS\Microsoft.NET\Framework\v3.5
directory for version 3.5, or the
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
directory for version 4.0.
Mono
[edit | edit source]For Windows, Linux, or other Operating Systems, an installer can be downloaded from the Mono website. The Mono C# compiler is called mcs.
Linux
[edit | edit source]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
[edit | edit source]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!
[edit | edit source]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 hello.cs
:
using System;
namespace MyConsoleApplication
{
class MyFirstClass
{
static void Main(string[] args)
{
//No need to use "System" but you can use it if you want but you already have "using System;"
System.Console.WriteLine("Hello,");
Console.WriteLine("World!");
Console.ReadLine();
}
}
}
To compile hello.cs
, 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
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe hello.cs
- For Mono run
mcs hello.cs
. - For users of cscc, compile with
cscc hello.cs -o hello.exe
.
Doing so will produce hello.exe
. The following command will run hello.exe
:
- On Windows, use
hello.exe
. - On Linux, use
mono hello.exe
orilrun hello.exe
.
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 hello.exe
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 System
namespace via the using
keyword. That inclusion allows direct references to any member of the System
namespace without specifying its fully qualified name.
The first call to the WriteLine
method of the Console
class uses a fully qualified reference.
System.Console.WriteLine("Hello,");
The second call to that method shortens the reference to the Console
class by taking advantage of the fact that the System
namespace is included (with using System
).
Console.WriteLine("World!");
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.