System.Console.Write
function
If you put the line "using static System.Console;
" at the top
of your file you can simply use "Write
" instead of the full name
"System.Console.Write
".
Compared to "Write
" the method "WriteLine
"
adds the newline symbol at the end of the output.
We shall be mostly using formatted output where the first argument
to "Write
" is a (format) string that specifies how things
must be written.
If the format string is the only argument, this string is simply sent
to the standard output (which by default is your terminal). For example,
Write("hello\n");
hello(the string "
\n
" represents the newline character).
Besides the first argument — the format string —
"Write
" can have more arguments which then must be the names
of the variables (or expressions) which you want to print out. In this
case the format string must contain placeholders for the arguments to
be printed. The string is then printed with placeholders replaced by
the values of the given variables (expressions).
By default the first argument after the format string takes the placeholder number zero "{0}", the second argument after the format string takes the placeholder number one "{1}", and so on.
For example
int n=123; Write("n = {0}, twice n = {1}, five times n = {2} \n", n, 2*n, 5*n);
produces
n = 123, twice n = 246, five times n = 615
The same output can also be achieved placing the dollar-sign, the string interpolation operator, before the string and writing the things to be printed out directly in the placeholders,
int n=123; Write($"n = {n}, twice n = {2*n}, five times n = {5*n} \n");The string interpolation operator replaces the expressions in the placeholders (inside the curly braces) with their string representations (it calls their "
ToString
" method, I believe).
The full description of System.Console.Write
method can be found [here].