
132
LESSON 11 Using Variables and Performing CalCUlations
You can use double quotes to surround strings and single quotes to surround chars as in the
following code:
string firstName = “William”;
string lastName = “Gates”;
char middleInitial = ‘H’;
Sometimes you might like to include a special character such as a carriage return or tab character in
a string literal. Unfortunately, you can’t simply type a carriage return into a string because it would
start a new line of code.
To work around this dilemma, C# provides escape sequences that represent special characters. An
escape sequence is a sequence of characters that represent a special character such as a carriage
return or tab.
Table 11-3 lists C#’s escape sequences.
TABLE 113
SEQUENCE MEANING
\a
Bell
\b
Backspace
\f
Formfeed
\n
Newline
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\‘
Single quotation mark
\“
Double quotation mark
\\
Backslash
\?
Question mark
\OOO
ASCII character in octal
\xhh
ASCII character in hexadecimal
\xhhhh
Unicode character in hexadecimal
For example, the following code makes a variable that refers to a string that contains quotes and a
newline character:
string txt = “Unknown value \“ten.\“\nPlease enter a number.”;
596906c11.indd 132 4/7/10 12:32:32 PM