Log inRegister

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
    Console.WriteLine
    to display text in the console
  • Execute multiple print statements in sequence
  • Control line breaks using
    Console.Write
    and
    Console.WriteLine
  • Embed special characters in strings using escape sequences
  • Write comments to document your code

Printing to the Screen

Create a file called

Program.cs
and type exactly this:

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.

Console
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.

WriteLine
is a method on
Console
. A method is a named action.
WriteLine
takes whatever you give it and prints it to the terminal, then moves to the next line. Write — a line.

"Hello, World!"
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.


Check Your Understanding

What does this print?

Console.WriteLine("C#");

Exercise
01-01 Hello World

Write a program that prints exactly this to the console:

Hello, World!
Log in to submit this exercise and get AI grading.

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

class Program
. No
public static void Main
. Just code.

Note

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

Main
is what actually runs. Everything else is required scaffolding that C# now writes for you.

Printing Multiple Lines

You can call

Console.WriteLine
as many times as you need:

Console.WriteLine("First line");
Console.WriteLine("Second line");
Console.WriteLine("Third line");

Output:

First line
Second line
Third 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.


Check Your Understanding

How many lines does this program print, and in what order?

Console.WriteLine("C");
Console.WriteLine("A");
Console.WriteLine("B");

Exercise
01-02 Three Lines

Write a program that prints these three lines, each on its own line, using three separate

Console.WriteLine
calls:

Once upon a time
I was falling in love
Now I'm only falling apart
Log in to submit this exercise and get AI grading.

Console.Write vs Console.WriteLine

There are two ways to print text in C#:

MethodBehaviour
Console.WriteLine
Prints the text, then moves to the next line
Console.Write
Prints the text and stays on the same line
Console.Write("Hello, ");
Console.Write("World");
Console.WriteLine("!");

Output:

Hello, World!

The two

Write
calls print without a line break. The final
WriteLine
adds the line break at the end. Useful when you want to build up a line in pieces.


Check Your Understanding

What does this program print?

Console.Write("A");
Console.Write("B");
Console.WriteLine("C");
Console.WriteLine("D");

Exercise
01-04 Stay on the Same Line

Use

Console.Write
and
Console.WriteLine
to produce this output on a single line:

C# is fun to learn.

Your program must use at least two separate print calls to produce that one line.

Log in to submit this exercise and get AI grading.

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.

SequenceMeaning
\n
New line
\t
Horizontal tab
\"
A literal double-quote character
\\
A literal backslash
Console.WriteLine("Line one\nLine two\nLine three");

Output:

Line one
Line two
Line three
Console.WriteLine("Name:\tAda Lovelace");

Output:

Name: Ada Lovelace
Note

There are no spaces around

\n
. If you write
"Line one \n Line two"
, the first line ends with a space and the second starts with one. That is usually not what you want.


Check Your Understanding

What does this print?

Console.WriteLine("A\nB\nC");

Exercise
01-21 Tab-Separated

Print the following line using a single

Console.WriteLine
call and the tab escape sequence
\t
. The words must be separated by a tab, not spaces.

Name Age City
Log in to submit this exercise and get AI grading.
Exercise
01-03 One Print, Three Lines

Print the same three lines as exercise 01-02 — but this time using a single

Console.WriteLine
call.

Once upon a time
I was falling in love
Now I'm only falling apart
Log in to submit this exercise and get AI grading.

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.


Check Your Understanding

How many lines does this program print?

// Console.WriteLine("First");
Console.WriteLine("Second");
/* Console.WriteLine("Third"); */
Console.WriteLine("Fourth");

Exercise
01-23 Comment Your Code

Write a program that prints three different facts about C# (your choice of facts). Add a single-line comment above each

Console.WriteLine
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.

Log in to submit this exercise and get AI grading.

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 compile
Console.WriteLine("Hello"); // ✓

Wrong capitalisation. C# is case-sensitive.

Console
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.writeline("Hello"); // ✗ both names are wrong
Console.Writeline("Hello"); // ✗ method name must match exactly
Console.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 here
Console.WriteLine("Hello"); // ✓

Using single quotes instead of double. Single quotes are for individual characters (

char
), not text strings. Writing
'Hello'
is a type error.

Console.WriteLine('Hello'); // ✗ syntax error — single quotes hold one char
Console.WriteLine("Hello"); // ✓

Exercise
01-22 Box of Stars

Print this pattern using exactly four

Console.WriteLine
calls:

****
* *
* *
****
Log in to submit this exercise and get AI grading.
Exercise
01-36 Poem

Write a program that prints a short four-line poem or rhyme of your choice. Use

Console.Write
for the last word of every other line so that those lines end without a newline before you add punctuation with a final
Console.WriteLine
. At least two lines must be built from multiple print calls.

Log in to submit this exercise and get AI grading.
Syntax Junkies