What the console is and why you print
The console is a text panel where a program shows messages: results, progress notes and error reports. Most of what your code does is invisible. Variables change, conditions are tested, loops run, and nothing appears on screen unless you ask for it. Printing is how you ask. It is also the quickest way to check what a program is doing while you learn.
The text a program prints is called its output.
Console.WriteLine
In C#, you print with the Console.WriteLine method. Put the value you want to print between the parentheses and end the statement with a semicolon:
Console.WriteLine(13);
Running it shows:
13
The pieces of that line each have a job:
Consoleis a class from theSystemnamespace that represents the console.WriteLineis a method of that class, reached with a dot.- The parentheses hold the argument, the value to print.
- The semicolon ends the statement. C# needs it after every statement.
The spelling matters. C# is case-sensitive, so console.WriteLine, Console.writeline and Console.Writeline are all errors.
The newline
WriteLine prints the value and then ends the line, as if you had pressed Enter. The next thing printed starts on a new line. That is why several calls produce several lines:
Console.WriteLine(1);
Console.WriteLine(2);
Console.WriteLine(3);
1
2
3
The line break comes after the value, so the output ends at the start of an empty line, but no blank line is printed between values. Calling WriteLine() with nothing in the parentheses prints only the line break, which gives you a blank line.
Its sibling Console.Write prints the value with no line break, so the next output continues on the same line:
Console.Write("a");
Console.Write("b");
Console.WriteLine("c");
abc
What you can print
You can pass numbers, text, and truth values. Text goes in double quotes, and the quotes are not printed:
Console.WriteLine(1998);
Console.WriteLine("hello");
Console.WriteLine(true);
Console.WriteLine(3 + 4);
Console.WriteLine("3 + 4");
1998
hello
True
7
3 + 4
Two things to notice. A bool prints with a capital letter, True. And 3 + 4 without quotes is an expression, so C# works out 7 first and prints that, while "3 + 4" in quotes is just text and is printed as written.
Running a snippet on your own machine
The lesson runs a snippet like Console.WriteLine(13); for you. In a project of your own, C# also needs a place to start. Modern C# (version 9 and later) allows top-level statements, so a file can begin with the statements themselves:
using System;
Console.WriteLine("Hello!");
The using System; line lets you write Console instead of System.Console. Newer project templates add it automatically. Older style puts the statements inside static void Main() in a class, and the call works the same way.
Common mistakes
- Missing semicolon.
Console.WriteLine(13)does not compile. - Wrong capitalisation.
consoleandwritelineare different names fromConsoleandWriteLine. - Missing parentheses.
Console.WriteLine 13;is not a method call. - Quotes around a number you want to compute.
"3 + 4"prints the text, not 7. - Expecting a blank line.
WriteLineends a line, it does not add an extra empty one.
The exercise
The task is to print the number 1998. It takes one statement:
- Write
Console.WriteLinewith the number in the parentheses. - Finish the line with a semicolon.
Console.WriteLine(1998);
The console then shows 1998 and moves to the next line, which is what the lesson's test checks. A number needs no quotes, and adding them would print the same characters but as text.
Key takeaways
- Printing is how you see what a program does.
Console.WriteLine(value);prints the value and then a line break.Console.Write(value);prints without the line break.- Text needs double quotes, and the quotes don't appear in the output.
- C# is case-sensitive, and every statement ends in a semicolon.