Your First Program
Writing your first program is a moment worth getting right. Let's not rush it.
Before writing any code, it helps to understand what a program actually is. A program is a list of instructions that a computer follows — in order, one at a time, exactly as written. Your job as a programmer is to write those instructions clearly enough that the computer can execute them without ambiguity.
C# is the language you are writing those instructions in. It runs on Windows, Mac, and Linux. It is used at companies like Microsoft, Unity, and hundreds of technology businesses. It is a powerful, modern, job-relevant choice.
By the end of this section you will be able to:
- Use
to display text in the consoleConsole.WriteLine - Execute multiple print statements in sequence
- Control line breaks using
andConsole.WriteConsole.WriteLine - Embed special characters in strings using escape sequences
- Write comments to document your code
Printing to the Screen
Create a file called
and type exactly this:Program.cs
Console.WriteLine("Hello, World!");
Run it. You should see:
Hello, World!
That is a complete, working C# program. One line.
Now let's look at what that line actually does, because understanding it will unlock everything that comes after.
is a built-in class that represents the terminal — the text window where your program runs. Think of it as the surface your program writes on.
is a method on
. A method is a named action.Console
takes whatever you give it and prints it to the terminal, then moves to the next line. Write — a line.WriteLine
is the text to print. The double quotes mark it as a string — a piece of text that the compiler treats as data, not as code. In C#, all text data is enclosed in double quotes.
— the semicolon ends the statement. Every instruction in C# ends with one. Without it, the program will not compile. Think of it as a full stop at the end of a sentence.
What does this print?
Console.WriteLine("C#");Write a program that prints exactly this to the console:
Hello, World!
Why No Class or Main Method?
If you have searched for C# code online, you have probably seen something like this:
using System;namespace MyProgram{class Program{public static void Main(string[] args){Console.WriteLine("Hello, World!");}}}
This is the old structure, and you will still find it everywhere because most tutorials and Stack Overflow answers were written before 2021.
Since .NET 5 (C# 9), C# supports top-level statements. The compiler automatically generates all that surrounding structure. You write the code that matters and the compiler handles the rest.
This course uses modern C# throughout. No
. Noclass Program
. Just code.public static void Main
When reading older tutorials or reference material you will see the full class/Main structure. It still works — it is just more typing than necessary. If you ever need to read it, know that the code inside
is what actually runs. Everything else is required scaffolding that C# now writes for you.Main
Printing Multiple Lines
You can call
as many times as you need:Console.WriteLine
Console.WriteLine("First line");Console.WriteLine("Second line");Console.WriteLine("Third line");
Output:
First lineSecond lineThird line
The program runs from top to bottom, one statement at a time, in the exact order you wrote them. This is called sequential execution, and it is the foundation of how all programs work.
How many lines does this program print, and in what order?
Console.WriteLine("C");
Console.WriteLine("A");
Console.WriteLine("B");Write a program that prints these three lines, each on its own line, using three separate
calls:Console.WriteLine
Once upon a timeI was falling in loveNow I'm only falling apart
Console.Write vs Console.WriteLine
There are two ways to print text in C#:
| Method | Behaviour |
|---|---|
| Prints the text, then moves to the next line |
| Prints the text and stays on the same line |
Console.Write("Hello, ");Console.Write("World");Console.WriteLine("!");
Output:
Hello, World!
The two
calls print without a line break. The finalWrite
adds the line break at the end. Useful when you want to build up a line in pieces.WriteLine
What does this program print?
Console.Write("A");
Console.Write("B");
Console.WriteLine("C");
Console.WriteLine("D");Use
andConsole.Write
to produce this output on a single line:Console.WriteLine
C# is fun to learn.
Your program must use at least two separate print calls to produce that one line.
Escape Sequences
Sometimes you need special characters inside a string — a new line, a tab, or a literal quote. C# handles these with escape sequences: a backslash followed by a special character.
| Sequence | Meaning |
|---|---|
| New line |
| Horizontal tab |
| A literal double-quote character |
| A literal backslash |
Console.WriteLine("Line one\nLine two\nLine three");
Output:
Line oneLine twoLine three
Console.WriteLine("Name:\tAda Lovelace");
Output:
Name: Ada Lovelace
There are no spaces around
. If you write\n
, the first line ends with a space and the second starts with one. That is usually not what you want."Line one \n Line two"
What does this print?
Console.WriteLine("A\nB\nC");Print the following line using a single
call and the tab escape sequenceConsole.WriteLine
. The words must be separated by a tab, not spaces.\t
Name Age City
Print the same three lines as exercise 01-02 — but this time using a single
call.Console.WriteLine
Once upon a timeI was falling in loveNow I'm only falling apart
Comments
Comments are notes written in your code that the compiler ignores completely. They are for humans — yourself, a teammate, or whoever reads your code six months from now.
// This is a single-line comment. Everything after // is ignored.Console.WriteLine("This runs."); // A comment can go at the end of a line./*This is a multi-line comment.Everything between the markers is ignored.Useful for longer explanations.*/Console.WriteLine("This also runs.");
Use comments to explain why you made a decision, not what the code does. The code already shows what it does. The reason behind a choice is what comments are for.
How many lines does this program print?
// Console.WriteLine("First");
Console.WriteLine("Second");
/* Console.WriteLine("Third"); */
Console.WriteLine("Fourth");Write a program that prints three different facts about C# (your choice of facts). Add a single-line comment above each
explaining what that line prints and why it is interesting. The comments are not visible in the output — the grader will read your source code.Console.WriteLine
Common Mistakes
Missing the semicolon. Every statement ends with
. Leave it out and the compiler will refuse to build the program with an error like;
.; expected
Console.WriteLine("Hello") // ✗ missing semicolon — will not compileConsole.WriteLine("Hello"); // ✓
Wrong capitalisation. C# is case-sensitive.
with a capital C is a built-in class.Console
with a lowercase c is an unknown name — the compiler will not recognise it.console
console.writeline("Hello"); // ✗ both names are wrongConsole.Writeline("Hello"); // ✗ method name must match exactlyConsole.WriteLine("Hello"); // ✓
Forgetting the quotes. A string must be wrapped in double quotes. Without them, the compiler tries to interpret the text as code and fails.
Console.WriteLine(Hello); // ✗ Hello is not a variable or keyword hereConsole.WriteLine("Hello"); // ✓
Using single quotes instead of double. Single quotes are for individual characters (
), not text strings. Writingchar
is a type error.'Hello'
Console.WriteLine('Hello'); // ✗ syntax error — single quotes hold one charConsole.WriteLine("Hello"); // ✓
Print this pattern using exactly four
calls:Console.WriteLine
***** ** *****
Write a program that prints a short four-line poem or rhyme of your choice. Use
for the last word of every other line so that those lines end without a newline before you add punctuation with a finalConsole.Write
. At least two lines must be built from multiple print calls.Console.WriteLine