Console IO

 
Chapter 2 - C# Basics
bySimon Robinsonet al.
Wrox Press 2002
  

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. During this chapter we have also used several of the Console class's static methods used for reading and writing data. Since these methods are so useful when writing basic C# programs, we will quickly go over them in a little more detail.

To read a character of text from the console window, we use the Console.Read() method. This will read an input stream from the console window and return the next character in the stream as an int . There are also two corresponding methods for writing to the console:

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

  • Console.WriteLine() - Which does the same, but adds a new line character at the end of the output.

There are various forms (overloads) of these methods for all of the predefined types (including object ), so in most cases we don't have to convert values to strings before we display them.

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

   int x = Console.Read();     Console.WriteLine((char)x);   

This is similar, but returns the entire line of text as a string:

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

Console.WriteLine() also allows us to display formatted output in a way comparable to C's printf() function. To use WriteLine() in this way, we 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 will display:

 10 plus 20 equals 30 

We 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, we 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, we 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 we will see in Chapter 5, it is possible to define your own format strings. However, the main ones in use for the predefined types are:

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 the most 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 wish 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, we 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:

 0.23 +   .70  ---------  ,013.93 

As a final trick, we 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#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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