Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Text Output from a Program

A console window is a simple command prompt window that allows a program to display text and receive input from the keyboard. The BCL supplies a class called Console (in the System namespace), which contains methods for inputting and outputting data to a console window.

Write

Write is a member of the Console class. It sends a text string to the program’s console window. In its simplest form, Write sends a literal string of text to the window. The string must be enclosed in quotation marks—double quotes, not single quotes.

The following line of code shows an example of using the Write member:

Console.Write("This is trivial text.");

Output string

This code produces the following output in the console window:

This is trivial text.

Another example is the following code, which sends three literal strings to the program’s console window:

System.Console.Write ("This is text1. ");

System.Console.Write ("This is text2. ");

System.Console.Write ("This is text3. ");

This code produces the output that follows. Notice that Write does not append a newline character after a string, so the output of the three statements runs together on a single line.

This is text1. This is text2. This is text3.

First

Second

Third

statement

statement

statement

23

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

WriteLine

WriteLine is another member of Console, which performs the same functions as Write but appends a newline character to the end of each output string.

For example, if you use the preceding code, substituting WriteLine for Write, the output is on separate lines:

System.Console.WriteLine("This is text 1.");

System.Console.WriteLine("This is text 2.");

System.Console.WriteLine("This is text 3.");

This code produces the following output in the console window:

This is text 1.

This is text 2.

This is text 3.

24

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

The Format String

The general form of the Write and WriteLine statements takes more than a single parameter.

If there is more than a single parameter, the parameters are separated by commas.

The first parameter must always be a string and is called the format string.

The format string can contain substitution markers.

A substitution marker marks the position in the format string where a value should be substituted in the output string.

It consists of an integer enclosed in a set of matching curly braces. The integer is the numeric position of the substitution value to be used.

The parameters following the format string are called substitution values. These substitution values are numbered, starting at 0.

The syntax is as follows:

Console.WriteLine( FormatString, SubVal0, SubVal1, SubVal2, ... );

For example, the following statement has two substitution markers, numbered 0 and 1, and two substitution values, whose values are 3 and 6, respectively.

 

Substitution markers

 

 

Console.WriteLine("Two sample integers are {0} and {1}.", 3, 6);

 

 

 

 

 

Format string

Substitution values

This code produces the following output on the screen:

Two sample integers are 3 and 6.

25

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Multiple Markers and Values

In C#, you can use any number of markers and any number of values.

The values can be used in any order.

The values can be substituted any number of times in the format string.

For example, the following statement uses three markers and only two values. Notice that value 1 is used before value 0 and that value 1 is used twice.

Console.WriteLine("Three integers are {1}, {0} and {1}.", 3, 6);

This code displays the following on the screen:

Three integers are 6, 3 and 6.

A marker must not attempt to reference a value at a position beyond the length of the list of substitution values. If it does, it will not produce a compile error but a runtime error (called an exception).

For example, in the following statement there are two substitution values, with positions 0 and 1. The second marker, however, references position 2—which does not exist. This will produce a runtime error.

Position 0

Position 1

 

 

Console.WriteLine("Two integers are {0} and {2}.", 3

6);

// Error!

 

 

There is no position 2 value.

26

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]