2. Create a new Console Application project by selecting File→New→Project.

3. Expand the Visual C# item on the left of the dialog, and select Windows. Then, select the Console Application template on the right (see Figure 3-1). Name the project HelloWorld.

Figure 3-1

4. Click OK. Figure 3-2 shows the skeleton of the console application.

Figure 3-2

5. Type the following highlighted code into the Main() method as shown:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace HelloWorld {

 class Program {

  static void Main(string[] args) {

   Console.WriteLine('Hello, world! This is my first C# program!');

   Console.ReadLine();

   return;

  }

 }

}

6. To debug the application and see how it looks like when executed, press F5 in Visual Studio 2008. Figure 3-3 shows the output in the Console window.

Figure 3-3 

To return to Visual Studio 2008, press the Enter key and the console window will disappear.

Using the C# Compiler (csc.exe)

Besides using Visual Studio 2008 to compile and run the application, you can build the application using Visual Studio 2008 and use the C# compiler (csc.exe) to manually compile and then run the application. This option is useful for large projects where you have a group of programmers working on different sections of the application.

Alternatively, if you prefer to code a C# program using a text editor, you can use the Notepad (Programs→Accessories→Notepad) application included in every Windows computer. (Be aware, however, that using Notepad does not give you access to the IntelliSense feature, which is available only in Visual Studio 2008.)

1. Using Notepad, create a text file, name it HelloWorld.cs, and save it into a folder on your hard disk, say in C:C#.

2. Populate HelloWorld.cs with the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace HelloWorld {

 class Program {

  static void Main(string[] args) {

   Console.WriteLine('Hello, world! This is my first C# program!');

   Console.ReadLine();

   return;

  }

 }

}

3. Use the command-line C# compiler (csc.exe) that ships with Visual Studio 2008 to compile the program. The easiest way to invoke csc.exe is to use the Visual Studio 2008 command prompt, which has all the path references added for you.

4. To launch the Visual Studio 2008 command prompt, select Start→Programs→Microsoft Visual Studio 2008→Visual Studio Tools→Visual Studio 2008 Command Prompt.

5. In the command prompt, change to the directory containing the C# program (C:C# for this example), and type the following command (see Figure 3-4):

C:C#>csc HelloWorld.cs

Figure 3-4

6. Once the program is compiled, you will find the HelloWorld.exe executable in the same directory (C:C#). Type the following to execute the application (see Figure 3-5):

C:C#>HelloWorld

Figure 3-5

7. To return to the command prompt, press Enter.

Dissecting the Program

Now that you have written your first C# program, let's take some time to dissect it and understand some of the important parts.

The first few lines specify the various namespaces:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

As mentioned in Chapter 1, all the class libraries in the .NET Framework are grouped using namespaces. In C#, you use the using keyword to indicate that you will be using library classes from the specified namespace. In this example, you use the Console class's WriteLine() method to write a message to the console. The Console class belongs to the System namespace, and if you do not have the using System statement at the top of the program, you need to specify the fully qualified name for Console, which is:

System.Console.WriteLine('Hello, world! This is my first C# program!');

The next keyword of interest is namespace. It allows you to assign a namespace to your class, which is HelloWorld in this example:

namespace HelloWorld {

 class Program {

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату