Console IO


Console I/O

By this point, you should have a basic familiarity with C#’s data types, as well as some knowledge of how the thread-of-control moves through a program that manipulates those data types. In this chapter, you have also used several of the Console class’s static methods used for reading and writing data. Because these methods are so useful when writing basic C# programs, this section quickly goes over them in a little more detail.

To read a line of text from the console window, you use the Console.ReadLine() method. This will read an input stream (terminated when the user presses the Return key) from the console window and return the input string. There are also two corresponding methods for writing to the console, which you have already used extensively:

  • Console.Write() - Writes the specified value to the console window.

  • Console.WriteLine() - This does the same, but adds a newline character at the end of the output.

Various forms (overloads) of these methods exist for all of the predefined types (including object), so in most cases you don’t have to convert values to strings before you display them.

For example, the following code lets the user input a line of text and displays that text:

  string s = Console.ReadLine(); Console.WriteLine(s); 

Console.WriteLine() also allows you to display formatted output in a way comparable to C’s printf() function. To use WriteLine() in this way, you pass in a number of parameters. The first is a string containing markers in curly braces where the subsequent parameters will be inserted into the text. Each marker contains a zero-based index for the number of the parameter in the following list. For example, {0} represents the first parameter in the list. Consider the following code:

  int i = 10; int j = 20; Console.WriteLine("{0} plus {1} equals {2}", i, j, i + j); 

This code displays:

 10 plus 20 equals 30

You can also specify a width for the value, and justify the text within that width, using positive values for right-justification and negative values for left-justification. To do this, you use the format {n, w}, where n is the parameter index and w is the width value:

  int i = 940; int j = 73; Console.WriteLine(" {0,4}\n+{1,4}\n ---- \n {2,4}", i, j, i + j); 

The result of this is:

   940 +  73  ----  1013

Finally, you can also add a format string, together with an optional precision value. It is not possible to give a complete list of possible format strings, since, as you see in Chapter 8, “Strings and Regular Expressions,” it is possible to define your own format strings. However, the main ones in use for the predefined types are shown in the following table.

Open table as spreadsheet

String

Description

C

Local currency format.

D

Decimal format. Converts an integer to base 10, and pads with leading zeros if a precision specifier is given.

E

Scientific (exponential) format. The precision specifier sets the number of decimal places (6 by default). The case of the format string (e or E) determines the case of the exponential symbol.

F

Fixed-point format; the precision specifier controls the number of decimal places. Zero is acceptable.

G

General format. Uses E or F formatting, depending on which is more compact.

N

Number format. Formats the number with commas as thousands separators, for example 32,767.44.

P

Percent format.

X

Hexadecimal format. The precision specifier can be used to pad with leading zeros.

Note that the format strings are normally case insensitive, except for e/E.

If you want to use a format string, you should place it immediately after the marker that gives the parameter number and field width, and separated from it by a colon. For example, to format a decimal value as currency for the computer’s locale, with precision to two decimal places, you would use C2:

  decimal i = 940.23m; decimal j = 73.7m; Console.WriteLine(" {0,9:C2}\n+{1,9:C2}\n ---------\n {2,9:C2}", i, j, i + j); 

The output of this in the United States is:

    $940.23 +   $73.70  ---------  $1,013.93

As a final trick, you can also use placeholder characters instead of these format strings to map out formatting. For example:

  double d = 0.234; Console.WriteLine("{0:#.00}", d); 

This displays as .23, because the # symbol is ignored if there is no character in that place, and zeros will either be replaced by the character in that position if there is one or else printed as a zero.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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