computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

📦

🎓

⏱ 25 min

Free

Variables, Types & Interpolation

Store data with int, double, bool, string and var, and build messages with $”{}”.

🎯 What you will learn

  • Declare typed variables
  • Use var to let C# figure out the type
  • Build strings with interpolation

👋 Let’s get started!

C# variables have types, like Java — but C# adds handy shortcuts like var and $”” strings ✨. You’ll use these in every game script.

🪜 Step by step

1. 🏷️ Types

Common types: int, double, bool, string, char.

int coins = 25;
double speed = 4.5;
bool isAlive = true;
string hero = "Kai";
2. 🔮 var

var lets the compiler infer the type from the value.

var level = 3;        // int
var title = "Knight"; // string
3. 💲 Interpolation

Put $ before a string and variables in { }.

Console.WriteLine($"{hero} has {coins} coins and speed {speed}");

🧪 Try it!

Read the code, guess what it prints, then press 👀 Show output.

Character stats

string name = "Kai";
int hp = 100;
double critChance = 0.15;
bool hasSword = true;

Console.WriteLine($"⚔️ {name}");
Console.WriteLine($"HP: {hp}");
Console.WriteLine($"Crit: {critChance:P0}");
Console.WriteLine($"Sword: {(hasSword ? "yes" : "no")}");

Collect a coin

int coins = 0;
coins += 10;
Console.WriteLine($"Coins: {coins}");

📖 Key words

Tap a card to flip it over!

var
Lets C# infer the type 🔮
string
Text (lowercase in C#) 🔤
Interpolation
$"text {variable}" ✨
Ternary ?:
A one-line if/else ❓

🏆 Challenge

Predict the output.

Challenge

int a = 7;
int b = 3;
Console.WriteLine($"{a} + {b} = {a + b}");
Console.WriteLine($"{a} / {b} = {a / b}");

Knowledge Check Quiz

  1. What type does var x = 4.2; become?
  2. What prints?
  3. Which stores true/false?
← Previous lesson
Next lesson →