Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11 Мб
Скачать
☆

CHAPTER 8 EXPRESSIONS AND OPERATORS

Note Real literals without a suffix are of type double, not float!

Character Literals

A character literal consists of a character representation between two single quote marks. A character representation can be any of the following: a single character, a simple escape sequence, a hex escape sequence, or a Unicode escape sequence.

•The type of a character literal is char.

•A simple escape sequence is a backslash followed by a single character.

•A hex escape sequence is a backslash, followed by an uppercase or lowercase x, followed by up to four hex digits.

•A Unicode escape sequence is a backslash, followed by an uppercase or lowercase u, followed by up to four hex digits.

For example, the following code shows various formats of character literals:

char c1

= 'd';

// Single

character

char c2

= '\n';

// Simple

escape sequence

char

c3

=

'\x0061';

//

Hex escape sequence

char

c4

=

'\u005a';

//

Unicode escape sequence

Table 8-3 shows some of the important special characters and their encodings.

Table 8-3. Important Special Characters

Name

Escape Sequence

Hex Encoding

Null

\0

0x0000

Alert

\a

0x0007

Backspace

\b

0x0008

Horizontal tab

\t

0x0009

New line

\n

0x000A

Vertical tab

\v

0x000B

Form feed

\f

0x000C

 

 

 

206

CHAPTER 8 EXPRESSIONS AND OPERATORS

Name

Escape Sequence

Hex Encoding

Carriage return

\r

0x000D

Double quote

\"

0x0022

Single quote

\'

0x0027

Backslash

\\

0x005C

 

 

 

String Literals

String literals use double quote marks rather than the single quote marks used in character literals. There are two types of string literals:

•Regular string literals

•Verbatim string literals

A regular string literal consists of a sequence of characters between a set of double quotes. A regular string literal can include the following:

•Characters

•Simple escape sequences

•Hex and Unicode escape sequences

Here’s an example:

string st1 = "Hi there!";

string st2 = "Val1\t5, Val2\t10";

string st3 = "Add\x000ASome\u0007Interest";

A verbatim string literal is written like a regular string literal but is prefaced with an @ character. The important characteristics of verbatim string literals are the following:

•Verbatim literals differ from regular string literals in that escape sequences are not evaluated. Everything between the set of double quotes—including what would normally be considered escape sequences—is printed exactly as it is listed in the string.

•The only exception with verbatim literals is sets of contiguous double quotes, which are interpreted as a single double quote character.

207

CHAPTER 8 EXPRESSIONS AND OPERATORS

For example, the following code compares some regular and verbatim string literals:

string rst1 = "Hi there!"; string vst1 = @"Hi there!";

string rst2 = "It started, \"Four score and seven...\""; string vst2 = @"It started, ""Four score and seven...""";

string rst3 = "Value 1 \t 5, Val2 \t 10";

//

Interprets tab esc

sequence

string vst3 = @"Value 1 \t

5, Val2 \t 10";

//

Does not interpret

tab

string

rst4

=

"C:\\Program

Files\\Microsoft\\";

 

 

string

vst4

=

@"C:\Program

Files\Microsoft\";

 

 

 

string rst5 = " Print \x000A Multiple \u000A Lines"; string vst5 = @" Print

Multiple

Lines";

Printing these strings produces the following output:

Hi there!

Hi there!

It started, "Four score and seven..."

It started, "Four score and seven..."

Value

1

5, Val2

10

Value

1

\t 5, Val2 \t 10

 

C:\Program Files\Microsoft\

C:\Program Files\Microsoft\

Print

Multiple

Lines

Print

Multiple

Lines

Note The compiler saves memory by having identical string literals share the same memory location in the heap.

208

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