Variables and Memory
What a variable actually is at the machine level, and how to picture variables, types, and assignments in C#
This is the first hands-on programming chapter. Everything that follows in the course builds on this single idea: a variable is a named box somewhere in memory that holds a value of a known type.
If you only take three things from this page, take these:
- A variable is a name the compiler gives to a piece of memory.
- Every variable in C# has a type — the compiler always knows it.
- Assignment
x = ycopies a value from one box into another (for value types) or copies a reference (for reference types — more on that next chapter).
Declaring and using variables
Read each declaration left to right:
int age = 30;— "make a variable namedage, of typeint, whose initial value is30."double price = 19.99;— "of typedouble" (floating-point number).string name = "Ada";— "of typestring."bool isAdmin = true;— "of typebool."
Once a variable has a type, it keeps that type forever. You
cannot later put a string into age.
int age = 30;
age = "thirty"; // ❌ does not compileThe compiler refuses. That refusal is a feature, not an annoyance — it prevents whole categories of bugs.
var: let the compiler figure it out
Repeating the type name on both sides can feel noisy:
List<string> names = new List<string>();C# lets you replace the left-hand type with var. The variable is
still statically typed — the compiler just figures the type out
from the right-hand side.
var is a convenience, not a way to opt out of types. age is
just as much an int as if you'd written int age.
What does a variable look like in memory?
Imagine memory as a long strip of numbered cells. When you write
int age = 30;, the compiler picks a cell (or a few cells), labels
it age, and writes the number 30 into it.
That picture is slightly oversimplified — strings and lists
actually live on the heap, with a small reference on the stack
pointing to them — but for value types like int and bool it's
exactly right.
When you write:
age = 31;…you don't make a new box. You overwrite the existing box.
age now holds 31.
Constants and read-only variables
Sometimes you want a value that you absolutely cannot change after it's set. C# gives you two tools.
const — set at compile time, never changes:
readonly — set once, usually in a constructor (we'll see this
later with classes). It's the same intent as const, but works
for values you can't know at compile time.
Use these freely. A variable that can't change is a variable that can't be wrong later.
Scope: where a variable is visible
A variable is only visible inside the block where it was
declared. A block is a section of code surrounded by { }.
This is a good thing. Limiting how widely a variable is visible makes programs much easier to understand. We will spend most of the course trying to make scopes as small as possible.
A trace of values changing
Let's do a small trace of how the variables in this program evolve:
Step-by-step view of memory:
Trace it on paper if it isn't obvious yet. Reading code in this "what's in each box right now?" way is one of the most valuable skills you can build.
Common primitive types you'll use constantly
| Type | Holds | Example literal |
|---|---|---|
int | 32-bit whole number | 42 |
long | 64-bit whole number | 42L |
double | 64-bit floating-point number | 3.14 |
decimal | High-precision decimal (money!) | 19.99m |
bool | true or false | true |
char | One Unicode character | 'A' |
string | Sequence of characters | "hello" |
A few small rules:
- For money, prefer
decimaloverdouble.doublehas rounding quirks for things like0.1 + 0.2. stringis technically a reference type but it acts like a value most of the time (we'll explain that in the next chapter).intis the default integer; use it unless you need something bigger.
If that output surprises you — great, that surprise is exactly why this rule matters.
Practice
Two variables a and b are declared and given values 10 and 20. Write code so that after your code runs, a holds the old value of b (i.e. 20) and b holds the old value of a (i.e. 10).
You must not change the Console.WriteLine lines at the bottom. The expected output is exactly:
a = 20
b = 10
Hint: you'll need a temporary variable.
Test your understanding
What does var x = 42; do?
It creates a variable with no type
It creates a variable whose type can change later
It creates a statically typed int variable; the compiler infers the type from the literal 42
It creates a runtime-only variable
Why prefer decimal over double for money?
decimal is faster
decimal uses less memory
double has small rounding errors for decimal fractions (e.g., 0.1 + 0.2 != 0.3); decimal is exact for the decimal numbers humans care about
They're the same — pick either one