c# · level 1 · unit 2 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Put something new in a box and the old thing is gone.
A box holds one value at a time. Put a new value in and the old one is thrown away. We say you it. Write the type only once, when you first make the box.
You can copy one box into another, like string order = drink;. You can also strings with +, which sticks them end to end, so mind the spaces.
A box can even use its own value. score = score + 1; takes what is in score, adds one and puts the answer back in.
using System;
string drink = "tea";
drink = "coffee";
Console.WriteLine("I would like " + drink);
int score = 5;
score = score + 1;
Console.WriteLine(score); Write the type again, like int score twice, and C# stops with CS0128, because that name is taken.
guess first
Copy, then change. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
string drink = "tea";
string order = drink;
drink = "coffee";
Console.WriteLine(order);
your turn
Your turn. Join a name together.
Type a first name and a last name between the quotes. Then press run and watch + join them, with the spaces in the right places.
output
press run to see what happens
fix it
You lost a life, but C# will not run. Can you fix it?
output
press run to see what happens
- Read the error on line 5. C# says the name lives is already defined.
- The box was made on line 3. Line 5 only needs to put a new value in it.
- Delete the word
intat the start of line 5, then press run.
nailed it
your boxes can change now. :D
- A new value overwrites the old one.
- Write the type only once, when you make the box.
+joins strings end to end, so mind the spaces.
new words in your collection
- overwrite
- join
This lesson isn’t saved. A free account remembers every lesson you finish.