c# · level 1 · unit 3 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Put $ before the quotes to fill gaps in your text.
Joining with + gets messy. Put $ before the opening quote instead, and anything inside {} is swapped for its value. This is string , like filling in the gaps on a birthday card.
A gap can hold a box name, like {name}, or a sum, like {age + 1}. C# works out the value and drops it into the text.
Forget the $ and C# leaves the curly brackets alone. It prints them exactly as you typed them.
using System;
string name = "Sam";
int age = 9;
Console.WriteLine($"Happy birthday, {name}! You are {age}.");
Console.WriteLine($"Next year you will be {age + 1}."); The $ goes right before the opening quote, with no space between them.
guess first
Spot the trap. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
string pet = "Rex";
Console.WriteLine("Good dog, {pet}!");
your turn
Your turn. See yourself in ten years.
Type your name between the quotes and your age as a whole number, then press run.
output
press run to see what happens
fix it
The score board shows a stray sign. Can you fix it?
output
press run to see what happens
- The gap was not filled in, and a
$appears on the screen. Where is the$on line 4? - The
$belongs outside the string, right before the opening quote. - Move the
$so line 4 readsConsole.WriteLine($"Score: {score}");, then press run.
nailed it
your text fills itself in now. :D
- Put
$right before the quotes to fill the gaps. - Curly brackets hold a box name or a sum.
- Without the
$, the curly brackets print as they are.
new words in your collection
- interpolation
- curly brackets
This lesson isn’t saved. A free account remembers every lesson you finish.