Modifying Your Simple C# Application

This section continues our introduction to C# programming with two examples that modify the example of Fig. 3.1 to print text on one line by using several statements and to print text on several lines by using only one statement.

Displaying a Single Line of Text with Multiple Statements

"Welcome to C# Programming!" can be displayed several ways. Class Welcome2, shown in Fig. 3.14, 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 1011 of Fig. 3.14.

Figure 3.14. Printing one line of text with multiple statements.

1 // Fig. 3.14: Welcome2.cs 2 // Printing one line of text with multiple statements. 3 using System; 4 5 public class Welcome2 6 { 7 // Main method begins execution of C# application 8 public static void Main( string[] args ) 9 { 10 Console.Write( "Welcome to " ); 11 Console.WriteLine( "C# Programming!" ); 12 } // end method Main 13 } // end class Welcome2  

Welcome to C# Programming!

The application is almost identical to Fig. 3.1. We discuss only the changes here. Line 2

// Printing one line of text with multiple statements.

is a comment stating the purpose of this application. Line 5 begins the Welcome2 class declaration.

Lines 1011 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 windowthe next character the application 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 statement resumes displaying characters from where the last Write statement displayed its last character.

Displaying Multiple Lines of Text with a Single Statement

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 in the console window. Like space characters and tab characters, newline characters are whitespace characters. The application of Fig. 3.15 outputs four lines of text, using newline characters to indicate when to begin each new line.

Figure 3.15. Printing multiple lines with a single statement.

1 // Fig. 3.15: Welcome3.cs 2 // Printing multiple lines with a single statement. 3 using System; 4 5 public class Welcome3 6 { 7 // Main method begins execution of C# application 8 public static void Main( string[] args ) 9 { 10 Console.WriteLine( "Welcome to C# Programming!" ); 11 } // end method Main 12 } // end class Welcome3  

Welcome to C# Programming!

Most of the application is identical to the applications of Fig. 3.1 and Fig. 3.14, so we discuss only the changes here. Line 2

// Printing multiple lines with a single statement.

is a comment stating the purpose of this application. 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. 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.16 lists several common escape sequences and describes how they affect the display of characters in the console window.

Figure 3.16. Some common escape sequences.

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.

Carriage return. Positions the screen cursor at the beginning of the current linedoes 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.

"

Double quote. Used to place a double-quote character (") in a string. For example,

Console.Write( ""in quotes"" );

displays

"in quotes"

Formatting Text with Console Write and Console WriteLine

Категории