quotation = ''I don't necessarily agree with everything I say.' Marshall McLuhan';
To represent the double-quote character in a string, you use the backslash () character to turn off its special meanings, like this:
string quotation =
''I don't necessarily agree with everything I say.' Marshall McLuhan';
Console.WriteLine(quotation);
The output is shown in Figure 8-1.

Figure 8-1
A backslash, then, is another special character. To represent the C:Windows path, for example, you need to turn off the special meaning of by using another , like this:
string path = 'C:\Windows';
What if you really need two backslash characters in your string, as in the following?
'\servernamepath'
In that case, you use the backslash character twice, once for each of the backslash characters you want to turn off, like this:
string UNC = '\\servername\path';
In addition to using the character to turn off the special meaning of characters like the double-quote (') and backslash (), there are other escape characters that you can use in strings.
One common escape character is the
. Here's an example:
string lines = 'Line 1
Line 2
Line 3
Line 4
Line 5';
Console.WriteLine(lines);
The
escape character creates a newline, as Figure 8-2 shows.

Figure 8-2
You can also use
to insert tabs into your string, as the following example shows (see also Figure 8-3):
string columns1 = 'Column 1 Column 2 Column 3 Column 4';
string columns2 = '1 5 25 125';
Console.WriteLine(columns1);
Console.WriteLine(columns2);

Figure 8-3
You learn more about formatting options in the section 'String Formatting' later in this chapter.
Besides the
and
escape characters, C# also supports the
escape character.
is the carriage return character. Consider the following example:
string str1 = ' One';
string str2 = 'Two';
Console.Write(str1);
Console.Write(str2);
The output is shown in Figure 8-4.

Figure 8-4
However, if you prefix a
escape character to the beginning of str2
, the effect will be different:
string str1 = ' One';
Console.Write(str1);
Console.Write(str2);
The output is shown in Figure 8-5.

Figure 8-5
The
escape character simply brings the cursor to the beginning of the line, and hence in the above statements the word 'Two
' is printed at the beginning of the line. The
escape character is often used together with
to form a new line (see Figure 8-6):
string str1 = 'Line 1
';
string str2 = 'Line 2
';
Console.Write(str1);
Console.Write(str2);

Figure 8-6
By default, when you use the
to insert a new line, the cursor is automatically returned to the beginning of the line. However, some legacy applications still require you to insert newline and carriage return characters in strings.
The following table summarizes the different escape sequences you have seen in this section:
Sequence | Purpose |
---|---|
| New line |
| Carriage return |
| Carriage return; New line |
' | Quotation marks |
\ | Backslash character |
| Tab |