This section continues our introduction to C# programming with two examples that modify the example of Fig. 3.1.
Class Welcome2
, shown in Fig. 3.10, uses two statements to produce the same output as that shown in Fig. 3.1. From this point forward, we highlight the new and key features in each code listing, as shown in lines 10–11 of Fig. 3.10.
The app is almost identical to Fig. 3.1. We discuss the changes here. Line 2
// Displaying one line of text with multiple statements.
states the purpose of this app. Line 5 begins the Welcome2
class declaration.
Lines 10–11 of method Main
Console.Write("Welcome to ");
Console.WriteLine("C# Programming!");
display one line of text in the console window. The first statement uses Console
’s method Write
to display a string. Unlike WriteLine
, after displaying its argument, Write
does not position the screen cursor at the beginning of the next line in the console window—the next character the app displays will appear immediately after the last character that Write
displays. Thus, line 11 positions the first character in its argument (the letter “C
”) immediately after the last character that line 10 displays (the space character before the string’s closing double-quote character). Each Write
or WriteLine
statement resumes displaying characters from where the last Write
or WriteLine
statement displayed its last character.
A single statement can display multiple lines by using newline characters, which indicate to Console
methods Write
and WriteLine
when they should position the screen cursor to the beginning of the next line. Like space characters and tab characters, newline characters are whitespace characters. The app of Fig. 3.11 outputs four lines of text, using newline characters to indicate when to begin each new line.
Most of the app is identical to the apps of Fig. 3.1 and Fig. 3.10, so we discuss only the changes here. Line 2
// Displaying multiple lines with a single statement.
states the purpose of this app. Line 5 begins the Welcome3
class declaration.
Line 10
Console.WriteLine("Welcome
to
C#
Programming!");
displays four separate lines of text in the console window. Normally, the characters in a string are displayed exactly as they appear in the double quotes. Note, however, that the two characters and
n
(repeated three times in the statement) do not appear on the screen. The backslash () is called an escape character. It indicates to C# that a “special character” is in the string. When a backslash appears in a string of characters, C# combines the next character with the backslash to form an escape sequence.2
The escape sequence
represents the newline character. When a newline character appears in a string being output with Console
methods, the newline character causes the screen cursor to move to the beginning of the next line in the console window. Figure 3.12 lists several common escape sequences and describes how they affect the display of characters in the console window.
Escape sequence | Description |
---|---|
|
Newline. Positions the screen cursor at the beginning of the next line. |
|
Horizontal tab. Moves the screen cursor to the next tab stop. |
" |
Double quote. Used to place a double-quote character (" ) in a string—e,g., Console.Write(""in quotes""); displays "in quotes" . |
|
Carriage return. Positions the screen cursor at the beginning of the current line—does not advance the cursor to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. |
\ |
Backslash. Used to place a backslash character in a string. |