Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Getting into the Visual Studio .Net Environment

Although writing C# programs using any text editor is possible, you will probably spend most of your time using the Visual Studio Integrated Debugging Environment (IDE). The Visual Studio IDE is based on earlier Microsoft languages, notably Visual Basic and Visual C++. One interesting feature of the .NET version of the IDE is that the same environment is used to program many languages. This is consistent with the tighter integration that now exists between the Microsoft languages. Now there are fewer differences between programs written in different languages in the Microsoft universe.

After Visual Studio .NET (sometimes referred to as Visual Studio 7) is loaded onto your machine, you activate it as you would any other program—from the Start menu.

As you can see from Figure 1.8, the IDE is a very complicated beast. Don’t worry about understanding the whole thing at once. I’ll show you the various parts as you need them. For now, rely on your experience as a software user. It’s reasonable to guess that the icons represent the most commonly needed functions in the program and that all the major commands are available through the online menu system. You might want to hover your mouse over the screen icons to find the important ones (such as the New Project button). For the most part, you write programs in the large gray area in the center of the screen. Everything else on the screen gives you information about what’s going on in the program or gives you access to tools such as the command line and various windows components. Because you aren’t going to use those features yet, you can leave them alone for now.

Figure 1.8: The Visual Studio IDE as it appears on my computer.

Starting a New Project

Start a new project by clicking—you guessed it—the New Project button, which lives in the upper−left corner of the screen. If you are averse to buttons, you can choose New, Project from the File menu. In either case, you see a dialog box that looks like Figure 1.9.

13

Figure 1.9: The New Project dialog box is where you determine the programming language, the project’s, and the type of project your are writing.

The New Project dialog box in Figure 1.9 has many important features. For example, the Project Types list box on the left enables you to determine which programming language you want to use. Depending on the way Visual Studio is configured on your system, you might have several other options. I currently have my machine configured for Visual Basic and C#. (I use other languages, too, but not usually in the .NET framework. Somehow it seems rude to use a Microsoft environment to write Perl code.) For the programs in this book, you always choose the C# environment.

Choosing the Project Type

After selecting the programming language, you can choose the type of project. You can use C# to write many types of programs. In the early stages of this book, you will write console applications, which are a simple interface because they are the easiest to understand. After you learn the basics of C# with these simple interfaces, you will graduate to Windows applications and eventually Web applications. For now, choose Console Application. However, be sure that you name your application and choose a location for it before pressing Enter or double−clicking the Console Application icon.

Trap If you double−click the Console Application icon before choosing a name or location for your project, Visual Studio assigns you a default name and location. It can be a real pain to fix this after the fact, so be sure that you type in a name and location before pressing Enter or clicking OK. I’ve made this mistake a number of times.

Examining the Code Window

After you determine the general characteristics of the program, the IDE starts writing code for you. All programs of a certain type share certain characteristics, so Visual Studio will supply boilerplate code to get you started. You can think of the automatically generated code as an outline that you can flesh out to write your program.

Figure 1.10 displays the code window as it appears after a new project named HelloWorld is

14

created. All the critical parts of any C# program are present, and the program will run, although it doesn’t do anything interesting yet.

Figure 1.10: The HelloWorld program displayed in the code window.

You have to learn a few things about C# before you start studying the code. Although Figure 1.10 doesn’t show it, the code is displayed in several colors. Words appear in blue, black, green, and gray. The colors indicate the type of information the compiler thinks each word is. For example, comments are in gray.

Also, you will note a certain symmetry to the text. Towards the beginning of the code are several left braces ({). Later in the code, you see matching right braces (}). The braces are used to group lines together. (I promise to show you exactly how. For now, I just want you to see the gestalt of the language so that you will understand later how the details fit together.) The braces are carefully matched so that every left brace has a right brace aligned directly underneath it (although sometimes several lines below the left brace) and everything inside the braces is indented. This is a common way of writing code in the languages derived from C, and because the IDE automates this style of code, you will stick with it now.

Trick A passionate discussion about vertically aligning your braces is ongoing in programming circles. To tell the truth, most languages (including C#) completely ignore the spacing and indentation in your code. The spaces help the programmer, not the computer. I prefer a different indentation convention, but because this form is built−in to the editor and is a reasonably standard approach, I will go with it for this book. The most important thing is to have a consistent style and stick with it. As you will see, indentation, commenting, and the like, can have a major effect on how well you get your programs to work.

You will also see minus signs to the left of the editor. When you click one of these symbols, you “collapse” the braces that follow the indicated line. This helps you to look at specific parts of your program and hide unnecessary details.

15

Examining the Default Code

As I just mentioned, the IDE starts to build your code for you. For your part, you will begin by examining the boilerplate code and later will add a little functionality. Here’s code that Visual Studio created:

using System;

namespace HelloWorld

{

///<summary>

///Summary description for Class1.

///</summary>

class Class1

{

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

}

}

}

This code is the same for any console−style application you write. Visual Studio gives you a starting place so that you don’t have to begin with a completely blank page. If you choose a different kind of application (like the Windows applications or Web applications you will write later in this book) the IDE will generate.

Adding a Reference to a Namespace

The first line given by the IDE says using System. The using statement indicates that a program will be using commands from a specific namespace. In a sense, the idea of namespaces is already familiar to you. At home, my wife calls me Andy. Calling me Andy Harris would be silly because everybody in our house is named Harris. At my job, there’s another guy named Andy, so people are more likely to say Andy Harris when they want to talk to me. You can always use a first name and a last name, but at home, your last name is implied.

Referring to a Namespace with a Using Statement

The using statement in C# works in a similar way. It enables you to use a group of commands that are related. You will see many namespaces in future chapters, but almost every program written in C# uses the System namespace because it contains useful objects. You need the console later, and the console object’s full name is System.Console. If you use the using System statement at the beginning of your program, you can simply refer to Console instead of System.Console. Almost every program in C# starts with the using System statement. As you learn more about C#, you will learn about other namespaces you will want to include in your programs.

Creating a Custom Namespace

The namespace HelloWorld line is used to generate your own namespace. In addition to the namespaces built in to the .NET environment, each project you create can have its own namespace. By default, the editor builds a namespace based on the project’s name. The namespace is called HelloWorld but actually contains all the code on the screen. You can see the left brace immediately after the namespace line. All the code is then indented until the

16

Соседние файлы в предмете Программирование на C++