In previous lessons, we saw that the Console class
allows using the Write() and the WriteLine() functions to display things
on the screen. While the Console.Write() method is used to display
something on the screen, the Console class provides the Read()
method to
get a value from the user. To use it, the name of a variable can be
assigned to it. The syntax used is:

VariableName = Console.Read();

This simply means that, when the user types something
and presses Enter, what the user had typed would be given (the word is assigned)
to the variable specified on the left side of the assignment operator.

Read() doesn't always have to assign its value to
a variable. For example, it can be used on its own line, which simply
means that the user is expected to type something but the value typed by
the user would not be used for any significant purpose. For example some
versions of C# (even including Microsoft's C# and Borland C#Builder) would display the DOS
window briefly and disappear. You can use the Read() function to wait for
the user to press any key in order to close the DOS window.

Besides Read(), the Console class also
provides the ReadLine() method. Like the WriteLine() member
function, after
performing its assignment, the ReadLine() method sends the caret to the
next line. Otherwise, it plays the same role as the Read() function.

In most assignments of your programs, you will not
know the value of a string when writing your application. For example, you
may want the user to provide such a string. To request a string (or any of
the variables we will see in this lesson), you can call the Console.Read()
or the Console.ReadLine() function and assign it to the name of the variable
whose value you want to retrieve. Here is an
example:

In C#, everything the user types is a string and the
compiler would hardly analyze it without your explicit asking it to do so.
Therefore, if you want to get a number from the user, first request a
string. Here is an example:

After getting the string, you must convert it to a number. To perform this
conversion, each data type of the .NET Framework provides a mechanism
called
Parse. To use
Parse(), type the data type, followed by a period, followed by Parse,
and followed by parentheses. In the parentheses of Parse, type the string
that you requested from the user. Here is an example:

After the user has entered the string you can then convert
it to a DateTime value. Just like any value you request from the user, a
date or time value that the user types must be valid, otherwise, the program
would produce an error. Because dates and times follow some rules for their
formats, you should strive to let the user know how you expect the value to be
entered.

By default, if you request only a date from the user and the
user enters a valid date, the compiler would add the midnight value to the date.
If you request only the time from the user and the user enters a valid time, the
compiler would add the current date to the value. Later on, we will learn how to
isolate either only the date or only the time.

Instead of using two Write() or a combination of Write() and
WriteLine() to display data, you can convert a value to a string and display it directly.
To do this, you can provide two strings to the Write() or WriteLine()
and separate them with a comma:

The first part of the string provided to Write() or WriteLine()
is the complete string that would display to the user. This
first string itself can be made of different sections:

One section is a string in any way you want it to
display

Another section is a number included between an opening curly
bracket "{" and a closing curly bracket "}". This
combination of "{" and "}" is referred to as a
placeholder

You can put the placeholder anywhere inside of the string. The first
placeholder must have number 0. The second must have
number 1, etc. With this technique, you can create the string anyway you
like and use the placeholders anywhere inside of the string

The second part of the string provided to Write() or WriteLine()
is the value that you want to display. It can be one value if you
used only one placeholder with 0 in the first string. If
you used different placeholders, you can then provide a
different value for each one of them in this second part, separating the
values with a comma

As mentioned already, the numeric value typed in the curly brackets of the first
part is an ordered number. If you want to display more than one value, provide
each incremental value in its curly brackets. The syntax used is:

Write("To Display {0} {1} {2} {n}", First, Second, Third, nth);

You can use the sections between a closing curly bracket and
an opening curly bracket to create a meaningful sentence.

We mentioned earlier that everything the user types using
the keyboard is primarily a string and it's your job to convert it to the
appropriate type. In reverse, if you have a value that is not a string, you can
easily convert it to a string. To support this, each .NET Framework data type
provides a mechanism called ToString. Normally, in C#, as we mentioned with
boxing, and as we have done so far, this conversion is automatically or
transparently done by the compiler. In some cases, you will need to perform the
conversion yourself.

To conversion a value of a primitive data type to a string,
type the name of the variable, followed by a period, followed, followed by ToString().
Here is an example:

To properly display data in a friendly and most familiar
way, you can format it. Formatting tells the compiler what kind of data you are
using and how you want the compiler to display it to the user. As it happens, you
can display a natural number in a common value or, depending on the
circumstance, you may prefer to show it as a hexadecimal value. When it comes to
double-precision numbers, you may want to display a distance with three values
on the right side of the decimal separator and in some cases, you may want to
display a salary with only 2 decimal places.

The System namespace provides a specific letter that you can
use in the Write() or WriteLine()'s placeholder for each category of data to
display. To format a value, in the placeholder of the variable or value, after
the number, type a colon and one of the appropriate letter from the following
table. If you are using ToString(), then, in the parentheses of ToString(), you
can include a specific letter or combination inside of double-quotes. The letters and their meanings are:

As you may have noticed, if you leave the parentheses
of ToString() empty, the compiler would use a default formatting to
display the value.

As opposed to calling ToString(), you can use
the above letters in the curly brackets of the first part of Write()
or WriteLine(). In this case, after the number in the curly
brackets, type the colon operator followed by the letter.

In the above programs, to display a line of text, we easily
used Write() or WriteLine(). To position text of different lengths
one above the other, we had to "corrupt" a string by including
extra-empty spaces. Such a technique is uncertain and less professional.
Fortunately, you can highly format how a string or a line of text should
display. The .NET Framework provides mechanisms to control the amount of space
used to display a string of text and how to align that string on its line.

To specify the amount of space used to display a string, you
can use its placeholder in Write() or WriteLine(). To do this, in
the placeholder, type the 0 or the incrementing number of the placer and its
formatting character if necessary and if any. Then, type a comma followed by the
number of characters equivalent to the desired width. Here are examples:

The sign you provide for the width is very important. If it
is positive, the line of text is aligned to the right. This should be your
preferred alignment for numeric values. If the number is negative, then the text
is aligned to the left.

Data and Time Formatting

As mentioned earlier, when the user enters a date value for
a DateTime variable, the compiler adds a time part to the value.
Fortunately, if you want to consider only the date or only the time part, you
can specify this to the compiler. To support this, the DateTime data type
provides a series of letters you can use to format how its value should be
displayed to the user. The character is entered in the placeholder of the DateTime
variable after the 0 or the incremental numeric value.