Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

3.4 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 display text on one line by using several statements and to display 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 10–11 of Fig. 3.14.

Fig. 3.14. Displaying one line of text with multiple statements.

 1   // Fig. 3.14: Welcome2.cs
 2   // Displaying 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 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 is a comment stating the purpose of this application. Line 5 begins the Welcome2 class declaration. Lines 10–11 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 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.

Fig. 3.15. Displaying multiple lines with a single statement.

 1   // Fig. 3.15: Welcome3.cs
 2   // Displaying 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\nto\nC#\nProgramming!" );
11      } // end 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 is a comment stating the purpose of this application. Line 5 begins the Welcome3 class declaration.

Line 10 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 \n 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.

Fig. 3.16. Some common escape sequences.

Escape sequence

Description

\n

Newline. Positions the screen cursor at the beginning of the next line.

\t

Horizontal tab. Moves the screen cursor to the next tab stop.

\r

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.

\"

Double quote. Used to place a double-quote character (") in a string—e,g.,

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

displays

       "in quotes"
  • + Share This
  • 🔖 Save To Your Account