Writing to the Console#
In a scratch program, you can print the result of an expression with
Console.WriteLine. This works for testing syntax and doing calculator-style
calculations. In a regular C# program run from a file like in
A Sample C# Program,
you must explicitly give instructions to print to a
console or terminal window.
This printing is accomplished through a function with a long name: Console.WriteLine.
As in math, you can pass a function a value to work on by placing it in
parentheses after the name of the function. Unlike in high school algebra classes,
in C# we have many types of data to supply other than numbers. The simplest
way to use the Console.WriteLine function is to give it a string.
We can demonstrate the statement and the line it prints:
Console.WriteLine("Hello, world!");
Output:
Hello, world!
What is printed to the screen does not have the quotes which we needed to
define the literal string "Hello, world!" inside the program.
Console is a C# class maintained by the system that
interacts with the terminal or console window where text output
appears for the program. A function defined in that class is WriteLine.
To refer to a function like WriteLine in a different class, you must indicate
the location of the function with the “dot” notation shown:
class name, then ., then the function. This
gives the more elaborate name needed in the program.
The string that gets printed can be the result of evaluating an expression, for instance concatenating:
int total = 5;
Console.WriteLine("All together: " + total);
Output:
All together: 5
More elaborate use of WriteLine is discussed in String Format Operation.
The Console.WriteLine function automatically makes the printing
position advance to the next line, as when you press the Enter or Return key.
A variant, Console.Write, prints the parameter exactly, and nothing else.
These differences are easiest to see in a complete program.
Printing is better shown in a real program.