Welcome to the taster course. In ten lessons you write your first programs in C#, and by the end a small notebook that runs on the console and keeps your notes in a file. You don’t need any prior knowledge, just a computer you’re allowed to install software on, or a free GitHub account for the version that runs in your browser.

The title promises ten hours. That’s about the time it takes to read and type along. With the exercises, better plan on a good twelve hours, spread over as many days as suits you. The minutes given for each lesson are guidelines. Feel free to note how long you actually took: later you’ll see for yourself how quickly you’re learning.

This first lesson needs no installation. It explains what a program actually is.

A program is a very precise set of instructions

Imagine a helper that is incredibly fast, never gets tired, and carries out every instruction exactly as it’s written. But it doesn’t think for itself. Tell it “Boil some water,” and it won’t ask how much, in which pot, or when to stop. It just stands there, or it does something you never meant.

A computer is this helper. A program is the set of instructions for it: a sequence of instructions that it carries out one after another. That’s why programming is above all about thinking precisely: What should happen, in what order, and what happens in the cases you don’t think of at first?

Almost every program does three things:

  1. It takes something in, an input: a keystroke, a piece of text, a file.
  2. It processes the input: calculates, compares, sorts, searches.
  3. It hands out a result, the output: text on the screen, a saved file, a message.

A calculator takes in numbers, calculates, and shows the result. A navigation app takes in your destination, finds a route, and shows it on the map. The notebook you build at the end of this course takes in notes, sorts them into a list, and shows them again.

From text to a running program

Programs are written as text in a programming language. This text is called source code, or code for short. People can read it, but your computer’s processor doesn’t understand it directly. In between sits a translator, the compiler. It checks your source code for errors and translates it into a form the computer can run.

In this course the language is C# (pronounced “C sharp”). C# is part of .NET, a free platform from Microsoft that runs on Windows, macOS, and Linux. .NET includes:

  • the compiler for C#,
  • the dotnet tool, which you use to translate and start programs,
  • a large collection of ready-made building blocks, for example for writing text to the screen or reading files.

Since version 10 of .NET, a single file is enough for a small program. You write the source code in a file whose name ends in .cs and start it with one command:

Bash
dotnet run hello.cs

This one command calls the compiler and then starts the finished program. The next lesson shows you how to set this up.

A first look at a C# program

Read the following program once, taking your time. You don’t have to run it yet or understand every character.

C#
// first-look.cs
Console.WriteLine("What is your name?");
string name = Console.ReadLine() ?? "";
Console.WriteLine($"Nice to meet you, {name}!");

Line by line, in plain English:

  1. // first-look.cs is a comment. The compiler skips everything after //. In this course, the first line of every example names the file you save it as.
  2. Console.WriteLine("What is your name?"); writes the question on the screen. Console stands for the text window the program runs in, WriteLine for “write a line.”
  3. string name = Console.ReadLine() ?? ""; waits until you type something and press Enter, and puts the text away in a storage spot named name. You’ll find out what ?? "" means in lesson 4.
  4. The last line writes a sentence with the stored name filled in.

That’s already a complete program with input, processing, and output, even if a very small one.

Mistakes are part of it

Everyone makes mistakes when programming, even after twenty years on the job. A forgotten semicolon, a typo in a name, a flaw in the order of your thinking. The compiler reports many of these mistakes before your program even starts, and tells you which line it got stuck on. These messages are hints, not grades. You’ll learn to read them in lesson 3.

By the way, much of the work isn’t typing but reading, thinking, and trying things out. If you notice that this is exactly what you enjoy, that’s a good sign.

How to work with this course

Every lesson is built the same way:

  • Goals at the start, so you know what matters.
  • Explanations in small steps with code you run yourself. Type the examples instead of copying them. Your fingers learn along with you, and you notice the small details that matter.
  • Exercises. Under each exercise there’s a solution that only opens when you click it. Try it yourself first, for ten or fifteen minutes if you need them. A solution of your own that looks different from ours can be just as right, as long as it does what was asked.
  • What you can do now: a short checklist.
  • Read more: links to Microsoft Learn, Microsoft’s free learning platform. Learn shows each page in your browser’s language where it has a translation.

At the end there’s a self-test with a scoring guide. You score it yourself. We don’t see your answers, there’s nothing to submit, and you don’t get a grade from us. The test is for you: it shows you what has stuck and what you should read again.

Exercise 1

On paper or in a text file, write instructions for the literal-minded helper from above. It gets a list of numbers, for example 4, 17, 9, 23, 8, and is supposed to tell you the largest one. It can remember one number, compare two numbers, and go through the list number by number, nothing more.

Then check your instructions yourself: Do they still work if the largest number comes first? If all the numbers are the same? If the list is empty?

Solution 1

One possible set of instructions:

  1. If the list is empty, say “There is no largest number” and stop.
  2. Remember the first number of the list as the “largest so far.”
  3. Go through the remaining numbers one by one. If a number is larger than the largest so far, remember that one instead.
  4. When you reach the end of the list, say the number you remembered.

With 4, 17, 9, 23, 8, the helper remembers 4, then 17, then 23, and says 23. If the largest number comes first, it sticks with the first one. If all of them are the same, it says that number. Step 1 is the case most people forget at first. Cases like this make the difference between a program that works most of the time and one that always works.

These instructions are already an algorithm: a precisely described way to a result. In lesson 7 you write it in C#.

Exercise 2

Look at first-look.cs once more. What appears on the screen if you type Ada when it asks? And what if you type nothing at all and press Enter right away?

Solution 2

With the input Ada it looks like this (the second line is what you typed):

What is your name?
Ada
Nice to meet you, Ada!

With no input, the name stays empty, and the program writes Nice to meet you, !. It does exactly what it says and doesn’t notice that something is missing. You’ll learn how a program can react to something like this in lesson 5.

What you can do now

  • You can say in your own words what a program is: a precise set of instructions with input, processing, and output.
  • You know that the compiler checks and translates source code, and that .NET comes with the compiler, the tool, and the building blocks.
  • You’ve read a first C# program and predicted what it does.
  • You know how to work with the exercises, the solutions, and the self-test.

Read more