This chapter provided a quick overview of the common features and tools available in Visual Studio 2008. Visual Studio 2008 is highly configurable, so you'll want to take some time to familiarize yourself with the environment. If you're totally new to C#, some Visual Studio features like code refactoring and unit testing may not seem all that important to you now, but once you've gotten some C# under your belt, you'll want to take another look at those features.
When you're ready, the next chapter gets you started in writing code in C#.
Chapter 3
C# Language Foundations
The best way to get started in a new programming language is to create a simple program and then examine the various parts that compose it. With this principle in mind, you'll create a simple C# program — first using Visual Studio 2008 and then using a plain text editor.
In this chapter you build and run the HelloWorld application, using Visual Studio 2008 as well as using the command line. After that, you tackle the syntax of the C# language and all the important topics, such as:
□ C# keywords
□ Variables
□ Constants
□ Comments
□ XML documentation
□ Data types
□ Flow control
□ Loops
□ Operators
□ Preprocessor directives
The easiest way to create your first C# program is to use Visual Studio 2008.
Editions of Visual Studio 2008
You can use any of the following editions of Visual Studio 2008 to create a C# program:
□ Visual C# 2008 Express Edition
□ Visual Studio 2008 Standard Edition
□ Visual Studio 2008 Professional Edition
□ Visual Studio 2008 Team Suite Edition
All the code samples and screen shots shown in this book were tested using Visual Studio 2008 Professional Edition.
1. Launch Visual Studio 2008.
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.
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 {
static void Main(string[] args) {
Console.WriteLine("Hello, world! This is my first C# program!");
Console.ReadLine();
Читать дальше