Home > Articles > Programming > C#

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

3.4. Modifying Your Simple C# App

This section continues our introduction to C# programming with two examples that modify the example of Fig. 3.1.

Displaying a Single Line of Text with Multiple Statements

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.

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

 1   // Fig. 3.10: 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# app
 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

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 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 app of Fig. 3.11 outputs four lines of text, using newline characters to indicate when to begin each new line.

Fig. 3.11 Displaying multiple lines with a single statement.

 1   // Fig. 3.11: 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# app
 8      public static void Main( string[] args )
 9      {
10        Console.WriteLine( "Welcome\nto\nC#\nProgramming!" );
11      } // end Main
12   } // end class Welcome3

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\nto\nC#\nProgramming!" );

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.12 lists several common escape sequences and describes how they affect the display of characters in the console window.

Fig. 3.12

Fig. 3.12 Some common escape sequences.

  • + Share This
  • 🔖 Save To Your Account