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

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net