Modifying Your Simple C# App

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 118 - 121)

2. When the app executes, another compiler (known as the just-in-time compiler

3.4 Modifying Your Simple C# App

correctly). Syntax errors occur for various reasons, such as missing parentheses and mis- spelled keywords.

When a syntax error occurs, the IDE underlines the location of the error with a red squiggly line and provides a description of it in theError Listwindow(Fig. 3.9). If the

Error List window is not visible in the IDE, select VIEW > Error List to display it. In Figure 3.9, we intentionally omitted the semicolon at the end of the statement in line 10.

The error message indicates that the semicolon is missing. You can double click an error message in theError Listto jump to the error’s location in the code.

3.4 Modifying Your Simple C# App

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

Error-Prevention Tip 3.4

One syntax error can lead to multiple entries in theError Listwindow. Each error that you address could eliminate several subsequent error messages when you recompile your app. So when you see an error you know how to fix, correct it and recompile—this may make several other errors disappear.

Fig. 3.9 | Syntax error indicated by the IDE.

Intentionally omitted semicolon (syntax error)

Squiggly underline indicates a syntax error Error description(s)

Error Listwindow

Displaying a Single Line of Text with Multiple Statements

ClassWelcome2, 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 states the purpose of this app. Line 5 begins theWelcome2class declaration.

Lines 10–11 of methodMain

display one line of text in the console window. The first statement usesConsole’s method

Writeto display a string. UnlikeWriteLine, after displaying its argument,Writedoesnot 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 thatWrite displays. Thus, line 11 positions the first character in its argument (the letter “C”) imme- diatelyafterthe last character that line 10 displays (the space character before the string’s closing double-quote character). EachWritestatement resumes displaying characters from where the lastWritestatement 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 toConsolemethodsWriteandWriteLinewhen they should position the screen cursor to the beginning of the next line in the console window. Like space characters and tab char- acters, 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.

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 11

12 } // end Main

13 } // end class Welcome2

Welcome to C# Programming!

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

// Displaying one line of text with multiple statements.

Console.Write( "Welcome to " );

Console.WriteLine( "C# Programming!" );

Console.Write( "Welcome to " );

Console.WriteLine( "C# Programming!" );

3.4 Modifying Your Simple C# App 79

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

states the purpose of this app. Line 5 begins theWelcome3class 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\andn(repeated three times in the statement) donotappear on the screen.

Thebackslash(\) is called anescape 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 anescape sequence.The escape sequence\nrepre- sents thenewline 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.

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 to C# Programming!" );

11 } // end Main

12 } // end class Welcome3

Welcome to C#

Programming!

Fig. 3.11 | Displaying multiple lines with a single statement.

// Displaying multiple lines with a single statement.

Console.WriteLine( "Welcome\nto\nC#\nProgramming!" );

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.

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

Console.Write("\"in quotes\"");displays"in quotes". Fig. 3.12 | Some common escape sequences. (Part 1 of 2.)

\n \n \n

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 118 - 121)

Tải bản đầy đủ (PDF)

(1.020 trang)