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

28

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

 

Chapter 1 Quick Reference

 

To

 

Do this

Key combination

Create a new console application

On the File menu, point to New, and then click

 

using Visual Studio 2008

Project to open the New Project dialog box.

 

Standard or Professional Edition

For the project type, select Visual C#. For the

 

 

 

template, select Console Application. Select a

 

 

 

directory for the project files in the Location

 

 

 

box. Choose a name for the project. Click OK.

 

 

 

 

Create a new console application

On the Tools menu, click Options. In the Options

 

using Visual C# 2008 Express

dialog box, click Projects and Solutions. In the

 

Edition

 

Visual Studio projects location box, specify a

 

 

 

directory for the project files.

 

 

 

On the File menu, click New Project to open the

 

 

 

New Project dialog box. For the template, select

 

 

 

Console Application. Choose a name for the

 

 

 

project. Click OK.

 

 

 

 

Create a new graphical application

On the File menu, point to New, and then click

 

using Visual Studio 2008 Standard

Project to open the New Project dialog box. For

 

or Professional Edition

the project type, select Visual C#. For the template,

 

 

 

select WPF Application. Select a directory for the

 

 

 

project files in the Location box. Choose a name for

 

 

 

the project. Click OK.

 

 

 

 

Create a new graphical application

On the Tools menu, click Options. In the Options

 

using Visual C# 2008 Express

dialog box, click Projects and Solutions. In the

 

Edition

 

Visual Studio projects location box, specify a

 

 

 

directory for the project files.

 

 

 

On the File menu, click New Project to open the

 

 

 

New Project dialog box. For the template, select

 

 

 

WPF Application. Choose a name for the project.

 

 

 

Click OK.

 

 

 

 

Build the application

On the Build menu, click Build Solution.

F6

 

 

 

Run the application

On the Debug menu, click Start Without

Ctrl+F5

 

 

Debugging.

 

 

 

 

 

Chapter 2

Working with Variables, Operators, and Expressions

After completing this chapter, you will be able to:

Understand statements, identifiers, and keywords.

Use variables to store information.

Work with primitive data types.

Use arithmetic operators such as the plus sign (+) and the minus sign (–).

Increment and decrement variables.

In Chapter 1, “Welcome to C#,” you learned how to use the Microsoft Visual Studio 2008 programming environment to build and run a Console program and a Windows Presentation Foundation (WPF) application. In this chapter, you are introduced to the elements of Microsoft Visual C# syntax and semantics, including statements, keywords, and identifiers. You’ll study the primitive types that are built into the C# language and the characteristics of the values that each type holds. You’ll also see how to declare and use local variables (variables that exist only in a method or other small section of code), learn about the arithmetic operators that C# provides, find out how to use operators to manipulate values, and learn how to control expressions containing two or more operators.

Understanding Statements

A statement is a command that performs an action. You combine statements to create methods. You’ll learn more about methods in Chapter 3, “Writing Methods and Applying Scope,” but for now, think of a method as a named sequence of statements. Main, which was introduced in the previous chapter, is an example of a method. Statements in C# follow a

well-defined set of rules describing their format and construction. These rules are collectively known as syntax. (In contrast, the specification of what statements do is collectively known as semantics.) One of the simplest and most important C# syntax rules states that you must ter-

minate all statements with a semicolon. For example, without its terminating semicolon, the following statement won’t compile:

Console.WriteLine(“Hello World”);

29

30

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

Tip C# is a “free format” language, which means that white space, such as a space character or a newline, is not significant except as a separator. In other words, you are free to lay out your statements in any style you choose. However, you should adopt a simple, consistent layout style and keep to it to make your programs easier to read and understand.

The trick to programming well in any language is learning the syntax and semantics of the language and then using the language in a natural and idiomatic way. This approach makes your programs more easily maintainable. In the chapters throughout this book, you’ll see examples of the most important C# statements.

Using Identifiers

Identifiers are the names you use to identify the elements in your programs, such as namespaces, classes, methods, and variables (you will learn about variables shortly). In C#, you must adhere to the following syntax rules when choosing identifiers:

You can use only letters (uppercase and lowercase), digits, and underscore characters.

An identifier must start with a letter (an underscore is considered a letter).

For example, result, _score, footballTeam, and plan9 are all valid identifiers, whereas result%, footballTeam$, and 9plan are not.

Important C# is a case-sensitive language: footballTeam and FootballTeam are not the same identifier.

Identifying Keywords

The C# language reserves 77 identifiers for its own use, and you cannot reuse these identifiers for your own purposes. These identifiers are called keywords, and each has a particular meaning. Examples of keywords are class, namespace, and using. You’ll learn the meaning of

most of the C# keywords as you proceed through this book. The keywords are listed in the following table.

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