Welcome back! Yesterday you made the computer say "Hello, World!" and (hopefully) survived the semicolon wars. Today we level up: we're going to teach the computer to remember things.
Because right now, your computer has the memory of a goldfish. You say "Hello" and it says "Hello" back. Ask it your name? Blank stare. Let's fix that.
1. What Even Is a Variable?
Think of a variable as a labeled cardboard box. You grab a box, slap a sticker on it that says "name," and then stuff something inside — like the string "Farhad". Later, whenever you need that value, you just shout the label and C# hands you the box.
The box has three important traits:
- A Type — what kind of thing fits in the box (numbers? text? true/false?)
- A Name — the sticker on the front
- A Value — the actual stuff inside
2. Declaring Your First Variable
Here's the grammar for declaring a variable in C#:
string name = "Farhad";
int age = 32;
bool isLearning = true;
Reading left-to-right: Type → Name → = → Value → semicolon. (Yes, the semicolon is still watching you. It never sleeps.)
Let's actually use one:
string name = "Farhad";
Console.WriteLine(name);
Run that, and the console proudly says Farhad. No quotes around name because we want what's inside the box, not the label itself.
3. The Main Primitive Types (Memorize These Five)
C# has a lot of types, but these five do 90% of the heavy lifting on Day 2:
| Type | Holds | Example |
|---|---|---|
int |
Whole numbers | int score = 42; |
double |
Numbers with decimals | double price = 9.99; |
bool |
true or false |
bool isReady = true; |
string |
Text (any length) | string city = "Baku"; |
char |
A single character in ' quotes |
char grade = 'A'; |
Gotcha alert: string uses double quotes "...", but char uses single quotes '...'. Mix them up and the secretary goes on strike again.
For the full list of built-in types, see the C# built-in types reference.
4. The Mysterious var Keyword
Typing string and int all day gets tedious. C# has a shortcut: var. It means "Hey compiler, figure out the type from the value on the right, I'm busy."
var name = "Farhad"; // compiler infers: string
var age = 32; // compiler infers: int
var price = 9.99; // compiler infers: double
Under the hood it's still strongly typed - once the compiler decides age is an int, you can't later jam a string into it. var is just a typing shortcut, not a free pass.
When to use var: when the type is obvious from the right-hand side. When NOT to use it: when the type would be genuinely unclear to a human reader.
5. Naming Rules & Conventions
C# has rules (the compiler enforces these) and conventions (the community enforces these, with judgment).
Rules (break these and it won't compile):
- Must start with a letter or underscore - never a digit.
2coolis illegal. - No spaces, no dashes, no emojis.
- Can't be a reserved keyword like
class,int, orreturn. - Case-sensitive:
name,Name, andNAMEare three completely different boxes.
Conventions (break these and your coworkers will judge you):
- camelCase for local variables:
firstName,totalPrice,isLoggedIn. - PascalCase for classes and public members:
Person,CalculateTotal. - Use descriptive names.
xis fine in math class. In code,userAgesaves future-you an hour.
6. Constants - Boxes You Can't Reopen
Sometimes you want a value that never changes. That's a const:
const double Pi = 3.14159;
const string AppName = "MyCoolApp";
Try to reassign Pi later and the compiler slaps your hand. Perfect for mathematical constants, config values, and anything you want to lock down.
7. String Interpolation - The Magic $ Sign
Yesterday you printed a plain string. Today we're going to inject variables into strings like professionals.
string name = "Farhad";
int age = 32;
Console.WriteLine($"Hello, my name is {name} and I am {age} years old.");
Output:
Hello, my name is Farhad and I am 32 years old.
The $ in front of the string tells C# "hey, I've got variables in here - look inside the { } and replace them." This is called string interpolation, and once you learn it, you'll never go back to the ugly "Hello, " + name + "!" concatenation.
8. Your Homework: An Actual Conversation
Make the computer ask the user their name and their age, then greet them. Use Console.ReadLine() to receive input (the opposite of WriteLine).
Console.Write("What's your name? ");
string name = Console.ReadLine();
Console.Write("How old are you? ");
string ageText = Console.ReadLine();
int age = int.Parse(ageText);
Console.WriteLine($"Hi {name}! Next year you'll be {age + 1}.");
Two new tricks hiding in there:
Console.ReadLine()always returns astring- even if the user types32.int.Parse(...)converts that string into an actual number so you can do math on it.
Bonus challenge: what happens if the user types "banana" when asked for their age? (Spoiler: the program explodes. We'll learn how to handle that in a future day.)
Summary of Day 2
- A variable is a labeled box that holds a value of a specific type.
- The big five types:
int,double,bool,string,char. varlets the compiler infer the type - it's still strongly typed.- Variable names are case-sensitive and follow camelCase.
constmakes a value immutable.- String interpolation with
$"..."is the clean way to build strings. Console.ReadLine()reads user input (always as astring).
Tomorrow: we'll talk about Operators & Expressions - all the +, -, *, /, ==, and && symbols that turn boring variables into actual logic. Bring a calculator.
See you on Day 3!