Console is a static class which is part of the System namespace. While the Console class contains an extensive collection of tools for manipulating the text-mode environment, these notes are primarily concerned with standard input and output.
In C#, "standard input" is a stream which is opened automatically for us. It will normally read from the keyboard.
Likewise, "standard output" is a stream which is opened automatically for us. It will normally write to a text window on the screen.
A few of the many available methods are:
This obtains the next character or function key pressed by the user. The key is displayed on the screen. Here ConsoleKeyInfo is a struct describing the key press (including combinations of keys and locks). To obtain the character, use ReadKey().KeyChar, where KeyChar is a property of ConsoleKeyInfo.
This is like ReadKey(), but if F is true, the key is not displayed.
This will read the next line of characters (up to the end-of-line marker, corresponding to someone pressing the Enter key).
It is often used just to pause until the user presses Enter.
This will write the text representation of the indicated object(s) to the standard output stream, using the (optional) Format string.
This writes an end-of-line character (and thus advances to the next line.) The end-of-line character is usually a carriage return followed by a linefeed (CR/LF).
This is like Write but afterwards adds an end-of-line character.
ToString()
This is a virtual method of the Object class and is overridden by methods of many classes. Its use is to provide the string representation of an object so it may be displayed. Usually this is a default format, but many classes overload ToString() to accept more parameters, typically formatting information (e.g., for culture-sensitive formats).
How do we read numbers?
Suppose we expect that the input line will contain an integer. We would like to have that value in an integer variable. Two ways to do this are:
int I = Convert.ToInt32(Console.Readline()); //convert string to int
Bear in mind that ToInt32 and Parse may throw a FormatException if the the input
string is not in the right format or an OverflowException if the value is outside
the range of Int32.
Here Convert is a class of static methods to convert strings to various other
types (and vice versa).
Composite Format String
It is common to use a formatting string with Write() or WriteLine(). In the
string, we somewhere have symbols {0}, {1}, etc. called "format items" which
will later be replaced by the values of the later arguments. This is known as a
"composite format string".
Example:
Here the value of {0} will be replaced by the text representation of N and
the value of {1} will be replaced by the text representation of X.
The composite format string can also be used elsewhere, as in this
example:
It is possible to have a fancier format than the default {0}. For
instance, in printing an integer, we might use {0:000}, which might
print 12345 or 00123. There are various possibilities.
What else is in Console?
There are properties in Console to get or set many details of the text window
in which we are working: the background color, the foreground color, the
position of the cursor (row and column), the status of the Caps Lock and Num
Lock, a title at the top of the window, redirect input or output, find out whether
input/output has been redirected, shift to the standard error stream or back to
standard input, etc.
There are methods in Console to do many of the same things as well as reset
the color scheme to the default, cause the speaker to beep (if desired, with
a specific frequency and duration), clear the window, etc.
int I = Int32.Parse(Console.ReadLine()); // likewise
int N = 5;
Char X = 'A';
WriteLine("The values are {0} and {1}.",N, X);
string primes;
primes = String.Format("Prime numbers less than 10: {0}, {1}, {2}, {3}",
2, 3, 5, 7 );
Console.WriteLine(primes);