Lesson 2 · ~12 min · ← Lesson 1
Variables & the core types
Every C# value has a type, decided the moment you write it. Learn the five you'll use daily, and the three traps that bite every beginner exactly once.
A variable is a labeled box of one shape
Declaring a variable = reserving a box, labeling it, and deciding what shape fits. Once decided, the shape never changes:
int age = 36; // whole numbers only
double height = 1.7; // decimal numbers
string name = "Ada Lovelace"; // text, in quotes
bool isProgrammer = true; // exactly true or false
Console.WriteLine(age);
Console.WriteLine(name);
Console.WriteLine(isProgrammer);
Read int age = 36; right to left: "make 36, put it in a box called age that only holds ints." The type is a promise — to the compiler and to every future reader.
The char you'll meet later: char
The fifth core type holds one character in single quotes. It's a different species from a 1-letter string:
char letter = 'a'; // single quotes = exactly one character
Console.WriteLine(letter);
Console.WriteLine(letter + 0); // surprise: a char IS its number (97 = code of 'a')
Console.WriteLine((char)(letter + 1)); // ...so char math works
var: same boxes, let the compiler label the shape
var doesn't mean "anything goes" — it means "compiler, look at the right side and fill in the type for me":
var price = 9.99; // compiler writes: double price = 9.99;
var count = 42; // compiler writes: int count = 42;
Console.WriteLine(price);
Console.WriteLine(count);
Proof it's not dynamic typing — try to put text in a var number in the playground: the compiler refuses exactly as if you'd written int. (Full story: var vs explicit types.)
Trap 1: int division throws the fraction away
Console.WriteLine(7 / 2); // both int → int math → 3
Console.WriteLine(7 / 2.0); // one double → double math → 3.5
This one has its own article: why 7 / 2 = 3 in C# — including negative numbers and the three fixes.
Trap 2: int has a ceiling — and it wraps
int biggest = int.MaxValue;
Console.WriteLine(biggest);
Console.WriteLine(biggest + 1); // wraps around to the most negative int!
An int is 32 bits: it can hold −2,147,483,648 … 2,147,483,647. Past the ceiling it wraps (like a car odometer). For bigger whole numbers, real code uses long (64 bits).
Trap 3: doubles are approximations
Console.WriteLine(0.1 + 0.2 == 0.3); // False! the sum is 0.30000000000000004…
Console.WriteLine((0.1 + 0.2).ToString("F2")); // 0.30 — display formatting hides it
Doubles store decimals in binary, and some decimal values (0.1!) have no exact binary form — like ⅓ has no exact decimal form. Display with formats like F2; never compare doubles with ==; for money, real code uses decimal.
Printing: joining values into text
Three ways, same result. Interpolation is the modern favorite:
string name = "Ada";
int age = 36;
Console.WriteLine(name + " is " + age); // concatenation: + glues text
Console.WriteLine(string.Format("{0} is {1}", name, age)); // placeholders
Console.WriteLine($"{name} is {age}"); // interpolation: $ + {…}
Practice this now
- In the playground: declare one variable of each of the five types and print them on one line with interpolation.
- Predict before running: what does
Console.WriteLine('a' + 'b');print? (Hint: chars are numbers.) - Then take the natural next lesson: Lesson 3 — Making decisions, or jump into the first practice problem.