First Program in C#

I

Contents

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.

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 cost money. The Visual Studio C# Express Edition can be downloaded and used for free from Microsoft's website.

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)
                {
                        //This is a comment
                        /*So
                        is
                        this*/
                        System.Console.WriteLine("Hello World!");
                        Console.WriteLine("Hello World!");

                        Console.ReadLine();
                }
        }
}
                                          

To compile hello.cs, run the following from the command line:

Doing so will produce hello.exe. The following command will run hello.exe:

Running hello.exe will produce the following output:

Hello World!
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 World!");
                                          

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("Hello 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.

 

 

All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks