Dataslope logoDataslope

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:

  1. A variable is a name the compiler gives to a piece of memory.
  2. Every variable in C# has a type — the compiler always knows it.
  3. Assignment x = y copies 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

Code Block
C# 13

Read each declaration left to right:

  • int age = 30; — "make a variable named age, of type int, whose initial value is 30."
  • double price = 19.99; — "of type double" (floating-point number).
  • string name = "Ada"; — "of type string."
  • bool isAdmin = true; — "of type bool."

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 compile

The 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.

Code Block
C# 13

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:

Code Block
C# 13

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 { }.

Code Block
C# 13

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:

Code Block
C# 13

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

TypeHoldsExample literal
int32-bit whole number42
long64-bit whole number42L
double64-bit floating-point number3.14
decimalHigh-precision decimal (money!)19.99m
booltrue or falsetrue
charOne Unicode character'A'
stringSequence of characters"hello"

A few small rules:

  • For money, prefer decimal over double. double has rounding quirks for things like 0.1 + 0.2.
  • string is technically a reference type but it acts like a value most of the time (we'll explain that in the next chapter).
  • int is the default integer; use it unless you need something bigger.
Code Block
C# 13

If that output surprises you — great, that surprise is exactly why this rule matters.

Practice

Challenge
C# 13
Swap two variables

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

QuestionSelect one

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

QuestionSelect one

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

On this page