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:
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:
This code displays:
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:
The result of this is:
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.
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:
The output of this in the United States is:
As a final trick, you can also use placeholder
characters instead of these format strings to map out formatting.
For example:
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.