In this lesson you write your first program of your own, make it fail on purpose, and fix it again. Afterward you know how the compiler reports problems and how to deal with its messages.

Hello, World

For decades, almost every programming course has started with a program that prints “Hello, World.” It’s small enough that almost nothing can go wrong, and yet it shows that the whole chain from text to output works.

In your csharp-k0 folder, create a file hello.cs and write this in it:

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

Save it and start the program in the terminal:

Bash
dotnet run hello.cs
Hello, World!

There’s also a short form, dotnet hello.cs. It does the same thing; in this course we always write the longer one, because it says more clearly what happens.

What happened? dotnet read your file and handed it to the compiler. The compiler checked it and translated it. Then dotnet started the translated program. It printed one line and ended. Start the program a second time: it’s faster, because nothing in the file has changed and the translation is already there.

What’s in the line

The line Console.WriteLine("Hello, World!"); is made up of several parts:

  • Console is the building block for the text window your program runs in.
  • The dot . means “from it”: you want to use something from Console.
  • WriteLine is a method, that is, something that does something. This one writes a line.
  • The parentheses ( ) hold what the method gets, here the text it should print.
  • The quotation marks " " mark where the text begins and ends. A piece of text in a program is called a string (a string of characters).
  • The semicolon ; ends the statement, the way a period ends a sentence.

C# is case-sensitive. To the compiler, Console and console are two different words, and it only knows the first one.

Several lines

A program carries out its statements from top to bottom, one after another:

C#
// business-card.cs
Console.WriteLine("Ada Sample");
Console.WriteLine("is learning to program");
Console.WriteLine();
Console.WriteLine("Favorite language: C#");
Ada Sample
is learning to program

Favorite language: C#

Console.WriteLine() without any text prints an empty line. Besides WriteLine there’s Write. It writes the text but doesn’t start a new line afterward:

C#
// one-two-three.cs
Console.Write("One, ");
Console.Write("two, ");
Console.WriteLine("three.");
Console.WriteLine("Four.");
One, two, three.
Four.

If you want a quotation mark itself in the text, put a backslash in front of it: \". Otherwise the compiler would take it for the end of the text.

C#
// quote.cs
Console.WriteLine("She said: \"I'll just give it a try.\"");
She said: "I'll just give it a try."

Comments

Everything after // up to the end of the line is a comment. The compiler skips it. Comments are for people, including you three weeks from now. A good comment explains why something is the way it is. What the code does, the code itself should show by being easy to read.

C#
// comments.cs
// The greeting comes first because it should be the first thing people see.
Console.WriteLine("Hello there!");
Console.WriteLine("Nice to have you here."); // A comment can also stand after code.

When the compiler says no

Now you build in a mistake on purpose. Create mistake.cs and leave out the semicolon:

C#
// mistake.cs
Console.WriteLine("Hello, World!")
Bash
dotnet run mistake.cs

Instead of the greeting, the compiler speaks up. It looks something like this (on your screen, the full path to your folder comes before the file name):

mistake.cs(2,35): error CS1002: ; expected

The build failed. Fix the build errors and run again.

That’s how it reads on a computer set to English and in Codespaces. On a computer set to another language, say German, the words are in that language, but the number CS1002 stays the same. Here’s how to read the message:

  • mistake.cs is the file.
  • (2,35) means line 2, character 35. That’s where the compiler noticed the problem, right after the closing parenthesis.
  • error means the program can’t be translated like this. It doesn’t even start.
  • CS1002 is the error’s number. It’s the same in every language. If you don’t understand a message, search the web for the number, for example “CS1002.” For most numbers, Microsoft has a page of its own that explains them.
  • After that, the message says in words what’s missing.

Put the semicolon back in, save, and start the program again. The greeting appears again.

Besides errors, the compiler sometimes reports warnings (warning). The program still runs, but the compiler has spotted something that probably isn’t what you meant. Take warnings as seriously as errors. In professional projects they’re often treated as errors, and every example in this course runs without a single warning.

How the computer knows whether everything went well

Every program reports a number back when it ends, the exit code. 0 means “everything’s fine,” any other number means “something went wrong.” You don’t see this number on the screen, but other programs read it, for example a tool that automatically checks whether your code still works. You can display it right after a program has run.

In PowerShell on Windows:

PowerShell
dotnet run hello.cs
$LASTEXITCODE

In the terminal on macOS, on Linux, and in Codespaces:

Bash
dotnet run hello.cs
echo $?

After hello.cs it shows 0. After mistake.cs with its missing semicolon it shows 1, because the translation failed. Remember this rule from the start: whether something worked is decided by the exit code, not by a message that looks friendly.

Exercise 1

Write a program my-card.cs that prints a small business card with your own details:

  • your name on the first line,
  • on the second line, what you do for a living or would like to do,
  • then an empty line,
  • then a line “I’m learning C# because …” with your reason. Build this last line from two Console.Write and one Console.WriteLine.

Solution 1

Your text will be different, of course. What matters is how it’s built:

C#
// my-card.cs
Console.WriteLine("Ada Sample");
Console.WriteLine("Accountant, maybe a developer soon");
Console.WriteLine();
Console.Write("I'm learning C# ");
Console.Write("because I want to find out ");
Console.WriteLine("whether programming is for me.");
Ada Sample
Accountant, maybe a developer soon

I'm learning C# because I want to find out whether programming is for me.

Watch the spaces at the end of the texts in the Write lines. Without them, the words stick together.

Exercise 2

This program has four mistakes. Save it as fix-me.cs, start it, and fix it with the help of the messages. The compiler reports several errors at once. Always fix the topmost one first, then start the program again: later messages are often just consequences of the first error.

C#
// fix-me.cs
Console.WriteLine("Good morning!);
console.WriteLine("How are you?");
Console.Writeline("See you soon.")

Solution 2

The four mistakes are:

  1. In line 2 the closing quotation mark is missing. The compiler reports CS1010 (“Newline in constant”): as far as it can tell, the text runs on to the end of the line.
  2. In line 3, console is written in lowercase. The compiler doesn’t know that name (CS0103).
  3. In line 4, Writeline has a lowercase l. Console has no method with that name (CS0117).
  4. In line 4 the semicolon is missing (CS1002).

Fixed, the program looks like this:

C#
// fix-me.cs
Console.WriteLine("Good morning!");
Console.WriteLine("How are you?");
Console.WriteLine("See you soon.");
Good morning!
How are you?
See you soon.

The messages come in waves. On the first run the compiler reports four errors in lines 2 to 4, among them CS1003 and CS1026 (a missing comma, a missing parenthesis). These two are only consequences of the missing quotation mark and go away with it. After that, only the missing semicolon is left. Only once that’s added too does the compiler report console and Writeline: it checks the names only once the grammar is right. That’s exactly why it pays to always fix the topmost error first and then start again.

Exercise 3

For a bonus: draw a frame of characters around your name, roughly like this:

+------------+
| Ada Sample |
+------------+

Solution 3

C#
// frame.cs
Console.WriteLine("+------------+");
Console.WriteLine("| Ada Sample |");
Console.WriteLine("+------------+");

For the frame to fit exactly, the top and bottom lines need as many dashes as your name is long, plus two for the spaces. With a different name, count again. In the next lessons you’ll see that the computer can work this out by itself.

What you can do now

  • You create a .cs file and start it with dotnet run.
  • You print text with Console.WriteLine and Console.Write, including text with quotation marks in it.
  • You write comments.
  • You read an error message: file, line, character, error number, text. You fix the topmost error first.
  • You know that exit code 0 means success and any other number an error.

Read more