Skip to content

Hello, C#

The Course
•
25 min study

A program is a list of instructions the computer follows in order. You can start C# here without knowing any other language, and without installing anything. Press Run to compile and execute the code; Reset restores the supplied version; Stop ends a long run. The first C# Run downloads about 14 MB of compiler files, then caches them.

C# is a compiled language. The compiler reads your whole program and checks it before a single line runs. If it finds a problem, nothing runs at all and you get an error message instead. That can feel strict, but it means many mistakes are caught before they can cause trouble.

Your first program

Console is the text window a program writes to. WriteLine prints a value and then moves to a new line. Text goes inside double quotes, and each statement ends with a semicolon. Statements run from top to bottom.

Before you press Run, write down what you think the output will be.

Predict before running C#
Console.WriteLine("Hello, C#!");
Console.WriteLine("Day 1");
Check your prediction
Text
Hello, C#!
Day 1

Swap the two lines and run again. The output changes order because the order of the statements is the order of the work.

Write and WriteLine

Console.Write prints without moving to a new line, so the next output continues on the same line. Anything after // on a line is a comment. The compiler ignores it; it is a note for people reading the code.

Where does each line break? C#
// Write stays on the current line.
Console.Write("Study ");
Console.Write("C#");
Console.WriteLine("!");
Console.WriteLine("Done");
Check your prediction
Text
Study C#!
Done

Reading a compiler error

Errors are not failure; they are the compiler telling you where to look. Each one has a position and a code. This program is missing a semicolon, so nothing prints, not even the first line.

Run this, then fix it C#
Console.WriteLine("First")
Console.WriteLine("Second");
Check your prediction
Text
error CS1002: ; expected

The numbers in brackets, such as (1,27), are the line and column. Add the semicolon and run it again.

C# is also case sensitive: Console and console are different names. A misspelled word inside quotes is only text, so the compiler won't notice it, but a misspelled name outside quotes is an error.

Spot the wrong capital C#
console.WriteLine("Hello");
Check your prediction
Text
error CS0103: The name 'console' does not exist in the current context

When there are several errors, fix the first one and run again. One small slip often causes a chain of messages, and the later ones may disappear on their own.

Your turn

Attempt the task with the hints closed. Then explain to yourself why each line appears where it does.

Exercise

Print two lines

Print Study C# and Start small on separate lines.

Your workspace C#
Console.WriteLine("Replace this line");
Hint 1: the idea

Each output line needs its own statement that ends the line.

Hint 2: a first step

Change the text in the supplied line so the first line is right, then run it before adding the second.

Show one solution
C#
Console.WriteLine("Study C#");
Console.WriteLine("Start small");
Text
Study C#
Start small

Each Console.WriteLine prints its text and then ends the line, so two statements give two lines. The text goes in double quotes, and every statement ends with a semicolon; leave either out and the compiler tells you where.

Close the solution and write the program again from memory. Then remove a quotation mark on purpose and read the error it causes.

Going further

Older C# code and many tutorials wrap the same statements in a class and a Main method. Both forms are normal. The short form you just used is called top-level statements, and the compiler generates the wrapper for you.

The same program, written out in full C#
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, C#!");
    }
}
Check your prediction
Text
Hello, C#!

Was this article helpful?

Your response is saved on this device.