Skip to content
Theory · Your first program · lesson 2 of 7

Hello, World!

In the previous lesson, a program was a recipe the computer follows from top to bottom. Now let's take apart one step of such a recipe and see how to print several lines.

Here is the one-line program again:

C#
Console.WriteLine("Hello, World!"); // Hello, World!

Let's take the line apart, from left to right:

  1. Console is the name of the console: the text window where a program prints and reads text. It is the same terminal window where you typed dotnet run.
  2. The dot after Console opens what the console can do, and WriteLine is one of its actions: "write a line".
  3. The parentheses hold what to print. The action's name together with its parentheses is called a call: that is how a program asks the console to do something.
  4. "Hello, World!" is text in double quotes, a string literal. The program prints it as it is, without the quotes.
  5. The semicolon ; ends the step.

Everything from Console to ; is one statement, that is, one step of the recipe. The file holds nothing but that statement, and that is enough: such a file is already a whole program.

Statements run in order

A program can have any number of statements. The computer runs them one after another, from top to bottom, and the next one starts only when the previous one has finished:

C#
Console.WriteLine("Heat the pan"); // Heat the pan
Console.WriteLine("Add the oil");  // Add the oil
Console.WriteLine("Fry an egg");   // Fry an egg

Swap two lines, and they swap in the output too: the computer runs the statements in the order they are written and looks for no other. Here the recipe and the program part ways. An experienced cook chops an onion while the pan heats up, but a program like this one runs strictly one statement at a time.

Statements are usually written one per line, so they are comfortable to read. The compiler, though, goes by the semicolons, not by line breaks: two statements on one line work too, as long as each ends with its own ;.

WriteLine and Write

There are two ways to print to the console. They differ in one thing only: what happens after the text.

WriteLine ends the line

Console.WriteLine prints its text and ends the line: the next output starts at the beginning of a new line. The call Console.WriteLine() with empty parentheses prints nothing and only ends the line.

Write keeps the line open

Console.Write prints its text and leaves the cursor on the same line. The cursor is the place where the next character will appear, so the next output continues that line:

C#
Console.Write("Hello, ");
Console.Write("World");
Console.WriteLine("!");   // Hello, World!
Console.WriteLine("Bye"); // Bye

Let's see what each statement does:

  1. Console.Write("Hello, ") prints Hello, together with the space at the end, and the cursor stays on the first line.
  2. The second Write adds World right after the space.
  3. WriteLine("!") adds the exclamation mark and ends the line: the first line of output is done, and it reads Hello, World!.
  4. The last statement prints Bye, already on the second line.

The space between the words sits in the first literal itself. Neither Write nor WriteLine adds anything between outputs: they print exactly what is inside the quotes.

In the widget below, a pancake recipe is printed step by step. Press "Next step" and watch the "output" line: after Write the text stays on the same line, and after WriteLine the next output starts on a new one.

LiveWrite and WriteLine step by stepstep 1 of 7

WriteLine prints "Pancakes" and ends the line.

The highlighted line is the one that ran on this step

The order here is the same as in a recipe: each statement runs after the previous one and prints its piece where the cursor stopped. Where a line ends is decided only by the choice between Write and WriteLine.

Comments

A comment is a note for people, and the compiler skips it. C# has two kinds:

  • // starts a comment that runs to the end of the line;
  • /* and */ enclose a comment that may span several lines.

A comment is a handy way to switch a statement off for a while: add // at the start of the line, and the compiler skips it.

Common mistake

Two words are printed by two calls, and the space is forgotten:

C#
Console.Write("Hello,"); // ✗
Console.WriteLine("World");

The program compiles and prints Hello,World—the words run together. Write adds no space after its text, and WriteLine adds none before its text. The space goes inside the literal, as in the example above: Console.Write("Hello, ");.

Key points

  • A program's statements run one after another, from top to bottom, and each ends with a semicolon.
  • Console.WriteLine prints text and ends the line, and Console.WriteLine() with empty parentheses only ends the line.
  • Console.Write prints text and leaves the cursor on the same line, so the next output continues it.
  • Neither Write nor WriteLine adds spaces: a space goes inside the string literal.