Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

xxviii Introduction

Support for This Book

Every effort has been made to ensure the accuracy of this book and the contents of the companion CD. As corrections or changes are collected, they will be added to a Microsoft Knowledge Base article.

Microsoft Press provides support for books and companion CDs at the following Web site:

http://www.microsoft.com/learning/support/books/

Questions and Comments

If you have comments, questions, or ideas regarding the book or the companion CD, or questions that are not answered by visiting the site above, please send them to Microsoft Press via e-mail to

mspinput@microsoft.com

Or via postal mail to

Microsoft Press

Attn: Microsoft Visual C# 2008 Step by Step Series Editor

One Microsoft Way

Redmond, WA 98052-6399

Please note that Microsoft software product support is not offered through the above addresses.

Microsoft Visual C# 2008 Step by Step

Part I

Introducing Microsoft Visual C#

and Microsoft Visual Studio 2008

In this part:

Chapter 1. Welcome to C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2. Working with Variables, Operators, and Expressions . . . . . . . . . . . . . 29 Chapter 3. Writing Methods and Applying Scope. . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chapter 4. Using Decision Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Chapter 5. Using Compound Assignment and Iteration Statements . . . . . . . . . . 85 Chapter 6. Managing Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

1

Chapter 1

Welcome to C#

After completing this chapter, you will be able to:

Use the Microsoft Visual Studio 2008 programming environment.

Create a C# console application.

Explain the purpose of namespaces.

Create a simple graphical C# application.

Microsoft Visual C# is Microsoft’s powerful component-oriented language. C# plays an important role in the architecture of the Microsoft .NET Framework, and some people have drawn comparisons to the role that C played in the development of UNIX. If you already know a language such as C, C++, or Java, you’ll find the syntax of C# reassuringly familiar. If you are used to programming in other languages, you should soon be able to pick up the syntax and feel of C#; you just need to learn to put the braces and semicolons in the right place. Hopefully, this is just the book to help you!

In Part I, you’ll learn the fundamentals of C#. You’ll discover how to declare variables and how to use arithmetic operators such as the plus sign (+) and minus sign (–) to manipulate the

values in variables. You’ll see how to write methods and pass arguments to methods. You’ll also learn how to use selection statements such as if and iteration statements such as while.

Finally, you’ll understand how C# uses exceptions to handle errors in a graceful, easy-to-use manner. These topics form the core of C#, and from this solid foundation, you’ll progress to more advanced features in Part II through Part VI.

Beginning Programming with the Visual Studio 2008 Environment

Visual Studio 2008 is a tool-rich programming environment containing all the functionality you need to create large or small C# projects. You can even create projects that seamlessly combine modules compiled using different programming languages. In the first exercise, you start the Visual Studio 2008 programming environment and learn how to create a console application.

Note A console application is an application that runs in a command prompt window, rather than providing a graphical user interface.

3

4

Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

Create a console application in Visual Studio 2008

If you are using Visual Studio 2008 Standard Edition or Visual Studio 2008 Professional Edition, perform the following operations to start Visual Studio 2008:

1.On the Microsoft Windows task bar, click the Start button, point to All Programs, and then point to the Microsoft Visual Studio 2008 program group.

2.In the Microsoft Visual Studio 2008 program group, click Microsoft Visual Studio 2008.

Visual Studio 2008 starts, like this:

Note If this is the first time you have run Visual Studio 2008, you might see a dialog box prompting you to choose your default development environment settings. Visual Studio 2008 can tailor itself according to your preferred development language. The various dia-

log boxes and tools in the integrated development environment (IDE) will have their default selections set for the language you choose. Select Visual C# Development Settings from the list, and then click the Start Visual Studio button. After a short delay, the Visual

Studio 2008 IDE appears.

If you are using Visual C# 2008 Express Edition, on the Microsoft Windows task bar, click the Start button, point to All Programs, and then click Microsoft Visual C# 2008 Express Edition.

Chapter 1 Welcome to C#

5

Visual C# 2008 Express Edition starts, like this:

Note To avoid repetition, throughout this book, I simply state, “Start Visual Studio” when you need to open Visual Studio 2008 Standard Edition, Visual Studio 2008 Professional Edition, or Visual C# 2008 Express Edition. Additionally, unless explicitly stated, all references to Visual Studio 2008 apply to Visual Studio 2008 Standard Edition, Visual Studio 2008 Professional Edition, and Visual C# 2008 Express Edition.

If you are using Visual Studio 2008 Standard Edition or Visual Studio 2008 Professional Edition, perform the following tasks to create a new console application.

1.On the File menu, point to New, and then click Project.

The New Project dialog box opens. This dialog box lists the templates that you can use as a starting point for building an application. The dialog box categorizes templates according to the programming language you are using and the type of application.

2.In the Project types pane, click Visual C#. In the Templates pane, click the Console Application icon.

6Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

3.In the Location field, if you are using the Windows Vista operating system, type

C:\Users\YourName\Documents\Microsoft Press\Visual CSharp Step By Step\Chapter 1. If you are using Microsoft Windows XP or Windows Server 2003, type C:\Documents and Settings\YourName\My Documents\Microsoft Press\Visual CSharp Step by Step\Chapter 1.

Replace the text YourName in these paths with your Windows user name.

Note To save space throughout the rest of this book, I will simply refer to the path “C:\ Users\YourName\Documents” or “C:\Documents and Settings\YourName\My Documents”

as your Documents folder.

Tip If the folder you specify does not exist, Visual Studio 2008 creates it for you.

4.In the Name field, type TextHello.

5.Ensure that the Create directory for solution check box is selected, and then click

OK.

If you are using Visual C# 2008 Express Edition, the New Project dialog box won’t allow you to specify the location of your project files; it defaults to the C:\Users\YourName\

AppData\Local\Temporary Projects folder. Change it by using the following procedure:

1.On the Tools menu, click Options.

2.In the Options dialog box, turn on the Show All Settings check box, and then click Projects and Solutions in the tree view in the left pane.

3.In the right pane, in the Visual Studio projects location text box, specify the

Microsoft Press\Visual CSharp Step By Step\Chapter 1 folder under your Documents folder.

4.Click OK.

If you are using Visual C# 2008 Express Edition, perform the following tasks to create a new console application.

1.On the File menu, click New Project.

2.In the New Project dialog box, click the Console Application icon.

3.In the Name field, type TextHello.

4.Click OK.

Chapter 1 Welcome to C#

7

Visual Studio creates the project using the Console Application template and displays the starter code for the project, like this:

The menu bar at the top of the screen provides access to the features you’ll use in the pro-

gramming environment. You can use the keyboard or the mouse to access the menus and commands exactly as you can in all Windows-based programs. The toolbar is located beneath

the menu bar and provides button shortcuts to run the most frequently used commands. The Code and Text Editor window occupying the main part of the IDE displays the contents of

source files. In a multi-file project, when you edit more than one file, each source file has its

own tab labeled with the name of the source file. You can click the tab to bring the named source file to the foreground in the Code and Text Editor window. The Solution Explorer dis-

plays the names of the files associated with the project, among other items. You can also double-click a file name in the Solution Explorer to bring that source file to the foreground in the Code and Text Editor window.

Before writing the code, examine the files listed in the Solution Explorer, which Visual Studio 2008 has created as part of your project:

Solution ‘TextHello’ This is the top-level solution file, of which there is one per application. If you use Windows Explorer to look at your Documents\Microsoft Press\Visual CSharp Step by Step\Chapter 1\TextHello folder, you’ll see that the actual name of this file is TextHello.sln. Each solution file contains references to one or more project files.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]