A computer's processor understands one kind of thing: very small instructions stored as numbers. "Add these two values." "Copy this over there." "If this is zero, skip ahead." Nobody writes a game, a banking app, or a school timetable that way. People write in a language they can read, and a tool translates it. This section is about that split, because every later chapter assumes you can keep the two sides apart.
Think about a recipe written in English for a cook who only reads a code
made of numbers. The recipe you write is the source code. It's just
text, and you can open it, read it, and change it. The language you
write it in, with its strict rules, is the
programming language. For this course that language is C#, said "C
sharp." A C# file's name ends in .cs, the way a photo's name might end
in .jpg.
The cook can't read English. So before anything happens, a translator goes through your recipe and rewrites it in the cook's number code. That translator is called the compiler. It's a program too, one that someone else wrote to turn your text into something that can run. With C#, the translated result is run by .NET (pronounced "dot net"), free software you'll install in Chapter 1.
Two things follow. You always have two kinds of file: the text you wrote and the files the compiler made from it. You change the first kind, never the second. And the compiler is strict. If one comma is out of place, it stops and tells you what's wrong instead of guessing. You get no program until you fix the text.
A program is a set of instructions a computer can carry out. A programming language is a strictly defined notation for writing
those instructions: every word and symbol has one agreed meaning, so a
tool can process the text without guessing. C# (said "C sharp") is one
such language. The text a person writes in it is source code, stored
in ordinary text files; C# source files end in .cs.
A processor cannot execute source code. A compiler is a program that reads source code, checks that it follows the language's rules, and translates it into a form that can run. For C#, the compiler writes program files, and a piece of software called .NET (said "dot net") loads those files and runs them on your machine. Chapter 1 opens that second step up; for now, two facts carry the rest of the course. First, the source code and the program are different files: you edit the source, never the output. Second, if the compiler finds a rule broken, it reports an error and produces no new program. Nothing runs until the text is fixed.
Written by you, or made by a tool?
Sort each card: is it source code a person writes, or something a tool produced from it?
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
hello.cs, Program.cs, and the readable Console.WriteLine line are source code. hello.dll, hello.exe, and the unreadable bytes are what the C# tools produce from that source.Worked example
Which file do you open to change the message?
-
1
A classmate built a program from
greeting.cs. When they look in the folder afterwards, they seegreeting.csplus a new folder full of files, includinggreeting.dll. -
2
They want the program to say "Good morning" instead of "Hello." The file with the words in it, typed by a person, is
greeting.cs. That's the source code. It's the one to open in an editor. -
3
greeting.dllis output. Opening it in a text editor shows scrambled symbols, because it was written for .NET to run, not for a person to read. Editing it by hand would, at best, change nothing and, at worst, break it. -
4
After changing
greeting.cs, they run the compiler again. It makes a freshgreeting.dllthat says "Good morning." The rule: change the source, then rebuild.
Check your understanding (counts toward your progress)
Why can't the processor run hello.cs just as it is?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Size has nothing to do with it. A one-line C# file is just as unreadable to a processor.
Any text editor opens a .cs file. Opening it isn't the problem; understanding it is.
Nothing is downloaded when a program runs. The translation happens on your machine.
Why
A processor carries out small instructions stored as numbers. C# is text written for people, so a compiler must translate it before anything can run.
What kind of instruction does a processor understand?
Lena changes Hello to Hi there in greet.cs and saves. She starts the program files she built yesterday, and they still print Hello. What's going on?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Saving is instant. The program never reads greet.cs at all; it only knows what it was translated from last time.
Programs aren't stuck with their first version. A fresh translation produces fresh program files.
The words inside quotes can be any language. The problem is the missing rebuild.
Why
Program files are a snapshot of the source at the moment it was compiled. Change the source, then compile again, or you keep running the old snapshot.
Which file does the program run from: the source or the files built from it?
What makes a programming language different from ordinary English?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
C# is full of words like Console and WriteLine. Pure numbers are what the processor reads after translation.
People read and write it every day; that's the point of source code.
The opposite. One misplaced symbol and the compiler refuses the file.
Why
A programming language has fixed, written-down rules, so a compiler can process the text without guessing. That strictness is why a single missing semicolon matters.
Put the steps in the order they happen, from idea to seeing output.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Write, save, translate, run, read. The compiler needs a saved .cs file to read, and .NET needs the compiler's output to run.
Correct answer
Also accepted:
Why
Nothing can be translated until it's been saved in a file.
Reading your first C# program
You don't need to know every symbol yet. Read a program the way you'd read a recipe in a language you half know: look for the words that carry meaning. Go top to bottom.
Each line of the program below is one instruction, and each one ends
with a semicolon (;), the way a sentence ends with a period. The
computer does the instructions in order: first line, then the second,
then the third. Then it stops.
Console.WriteLine means "write a line on the screen." The console
is the plain text window a program prints into. Whatever you put inside
the round brackets is what gets printed, and afterwards the next print
starts on a fresh line, like pressing Enter.
If the thing in the brackets is inside double quotes, it's printed
letter for letter, spaces and all. If it isn't in quotes, like 3 + 4,
the computer works it out first and prints the answer, 7.
Capital letters matter. Console with a capital C works. console
with a small c is a different word that C# doesn't know. The compiler will refuse
the whole file.
A C# program is made of statements, each a single instruction, and
each ending in a semicolon (;). In a small program written this way
(called top-level statements), the statements run once each, in order,
from the top of the file to the bottom. Then the program ends.
Console.WriteLine(...) is the statement that prints. Whatever sits
between its parentheses is written to the screen, followed by a line
break, so the next print starts on a new line. Text in double quotes
("Hello!") is printed exactly as typed, spaces and punctuation
included. Anything else between the parentheses is worked out first, and
the result is printed: Console.WriteLine(3 + 4) prints 7.
C# is case-sensitive: Console and console are different names,
and only the first is the one .NET provides.
Console.WriteLine("Hello!");
Console.WriteLine("Welcome to C#.");
Console.WriteLine(3 + 4);
Hello!
Welcome to C#.
7
Worked example
Reading it line by line
-
1
Console.WriteLine("Hello!");prints the text in quotes,Hello!, and ends the line. The;ends the instruction. -
2
Console.WriteLine("Welcome to C#.");runs next, because statements run top to bottom. It printsWelcome to C#.on the second line, full stop included, since everything inside the quotes is copied as is. -
3
Console.WriteLine(3 + 4);has no quotes, so C# works out3 + 4first and prints the result,7, on the third line. -
4
After the last line there's nothing left to do, so the program ends. Three instructions, three lines of output.
Predict before you peek
Type exactly what the program prints, one line per line. The answer was produced by the real .NET SDK.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
2 + 5 is printed as those three characters. The third has no quotes, so C# adds 2 and 5 and prints 7.Worked example
Quotes change everything
-
1
Compare these two instructions:
Console.WriteLine("3 + 4");andConsole.WriteLine(3 + 4);. The only difference is the pair of quotes. -
2
With quotes,
3 + 4is text, a label. C# doesn't look inside it; it prints the five characters3 + 4as they are. -
3
Without quotes,
3 + 4is a calculation. C# works it out and prints7. -
4
A useful habit when you predict output: before anything else, ask "is this in quotes?" That one question settles most early predictions.
A program can also keep a number under a name and use the name later. You'll meet this properly in Chapter 2; here it's enough to read it.
int apples = 3;
int oranges = 4;
Console.WriteLine(apples + oranges);
7
int apples = 3; stores the whole number 3 under the name apples
(int is short for integer, a whole number). The last line adds the
two stored numbers and prints the total.
Practice: Read a program top to bottom (4 problems · doesn’t affect your progress)
What does Console.WriteLine("8 + 2"); print?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
C# only calculates when the sum isn't inside quotes. Here it is, so it's just text.
The quotes mark where the text starts and ends. They aren't printed themselves.
It's valid C#. Text with symbols in it is still fine to print.
Why
Everything between the quotes is printed as written, and the quotes themselves are not printed. Remove the quotes and it would print 10.
The five characters 8 + 2
A bakery's stock program prints a label, a sum in quotes, and the same sum without quotes. What appears on the screen?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The first two lines are quoted, so they print as typed, 20 - 6 included. The third line has no quotes, so C# subtracts and prints 14.
Correct answer
Also accepted:
Why
Ask of each line: is the thing in the brackets inside quotes?
Cakes left: ⏎ 20 - 6 ⏎ 14
The steps are labelled out of order. What does the program print, line by line?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Statements run in the order they appear in the file. The numbers are inside quotes, so C# treats them as plain text and doesn't sort anything.
Correct answer
Also accepted:
Why
Does C# read the numbers inside the quotes, or just run the lines in order?
Step 1 ⏎ Step 3 ⏎ Step 2
The middle line has nothing between its brackets. Type exactly what the program prints, including any empty line.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Console.WriteLine() with nothing inside prints just the line break, so an empty line appears between the other two.
Correct answer
Also accepted:
Why
WriteLine always ends the line. What's left if there's nothing to write first?
Name: Ada ⏎ ⏎ Score: 90
Common mistakes: the semicolon and the capital letter
Two mistakes account for most first-day errors. Both stop the compiler,
so the program never runs. The compiler reports each one with an error
code that starts CS; Chapter 1 shows you how to read those messages.
A missing semicolon. The compiler reports error CS1002, "; expected":
Console.WriteLine("Hello!")
Console.WriteLine("Bye!");
A small letter where a capital belongs. The compiler reports error
CS0103: it has never heard of anything called console:
console.WriteLine("Hello!");
Bug hunt: the program that won't build
Fix the 1 mistake so this program builds and prints both lines.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
; after the closing bracket.Worked example
Fixing a program that won't build
-
1
A student types a program to print a receipt heading and gets an error instead of output. The file says
Console.WriteLine("Receipt")on line 1 andConsole.WriteLine("---");on line 2. -
2
Nothing was printed at all, not even the first line. That tells you the problem was caught by the compiler before the program ran: a translation problem, not a running problem.
-
3
Read each line's ending. Line 2 ends in
;. Line 1 ends in)with no semicolon, so C# can't tell where the first instruction stops. -
4
Add the
;, build again, and both lines print. When a program prints nothing at all and shows an error, look at the text before you look anywhere else.
Professional habit: when the compiler reports several errors, fix the first one and build again. One missing semicolon can confuse the compiler about everything after it. Later errors often disappear on their own once it's fixed.
Where this goes next
Chapter 1 §3 has you type a program like these into hello.cs. You run
it with one command, dotnet run hello.cs, which translates and runs in
a single step. Chapter 1 §7 opens the translator box in the figure and
shows what the program files contain. Chapter 1 §9 teaches you to read
the CS1002 and CS0103 messages you just met.
Check your understanding (counts toward your progress)
This shopping list won't build. Fix the 1 mistake.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Line 2 has no ; at the end, so the compiler can't tell where that instruction stops and reports CS1002, "; expected". Adding the semicolon fixes it.
Correct answer
Also accepted:
Why
Compare how each line ends.
Four tickets cost 12 each. What does the program print?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
tickets holds 4 and price holds 12. tickets * price is 4 × 12, so the program prints 48.
Correct answer
Also accepted:
Why
In C#, * means multiply.
The compiler says it doesn't know a name on line 2. Fix the 1 mistake.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
C# is case-sensitive. console with a small c is a name C# has never heard of (error CS0103); Console with a capital C is the one that prints.
Correct answer
Also accepted:
Why
Line 1 works. Look at the very first letter of line 2.
A theatre's sign program has two lines. What appears on the screen?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Two Console.WriteLine statements, run top to bottom, each printing its quoted text on its own line.
Correct answer
Also accepted:
Why
Practice: Read a C# program (5 problems · doesn’t affect your progress)
In Console.WriteLine("Hi");, what job does the ; do?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Only what's inside the quotes is printed. The ; sits outside them.
One file is one program. The ; just closes one instruction.
The console has no bold text. The ; is punctuation for the compiler.
Why
Each C# instruction ends with ;, the way a sentence ends with a full stop.
It ends that instruction
A one-line greeting program. What does it print?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
One statement, one line of output: the quoted text as written.
Correct answer
Also accepted:
Why
Good morning
A fruit-stall program prints two labels and a sum. What's the output?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The first two lines are quoted text. The third has no quotes, so C# adds 6 and 9 and prints 15.
Correct answer
Also accepted:
Why
Which line has no quotes?
Apples: 6 ⏎ Pears: 9 ⏎ 15
The home team scored 18 and the away team 7. Fill the blank so the program prints the total, 25.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
home + away adds the two stored numbers, 18 + 7, and prints 25. In quotes it would print the words home + away instead.
Correct answer
Also accepted:
Why
Use the two stored names, not the numbers, and don't put quotes around them.
home + away
This program has 2 mistakes. Fix both so it builds and prints Seats left: and then 12.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Line 1 needs its ;, and line 2 must be WriteLine with a capital L. C# treats Writeline as a different, unknown name.
Correct answer
Also accepted:
Why
One mistake is at the end of a line; the other is a single letter that should be a capital.