A named box in memory: what an int stores, what a string stores, and why two objects can share one value
What is a variable? Every program takes input, processes it and gives output, and the input has to be kept somewhere while the program works. That somewhere is a variable.
In this lesson we see what a variable is, the two kinds of variables (value types and reference types), and what actually happens in memory when you write int num1 = 12 or string name = "Sunil".
What is a variable?
Break the word apart: vary + able, something that is able to change. A variable is a named place in memory that holds a value, and that value can change while the program runs.
Think of a program that adds two numbers. The user might type 12 and 13 today and 5 and 6 tomorrow. If the numbers were fixed in the code, the program could only ever add those two. Variables keep the program flexible: num1 and num2 hold whatever the user types.
Two kinds of variables: value types and reference types
| | Value type | Reference type |
|—|—|—|
| What the variable holds | the value itself | the address of the value |
| Examples in C# | int, double, bool, char, DateTime | string, arrays, class objects |
| b = a copies | the value | the reference (both point to one object) |
Every type also has a size. In C#, an int is 32 bits (4 bytes) and a double is 64 bits (8 bytes).
How a number is stored: 16 in binary
Memory only stores bits: each bit is a 0 or a 1. So a number is first written in binary. Divide by 2 and note the remainders:

16 / 2 = 8 remainder 0
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 (last quotient)
Read from the bottom up: 16 = 10000. (Check: 1 x 16 + 0 + 0 + 0 + 0 = 16.)
A value type in memory: int num1 = 12
When you declare int num1, the compiler reserves 32 bits (4 bytes) for it. Memory is divided into bytes, and every byte has its own address, so num1 is simply a name for that address.
Assigning num1 = 12 writes 12 in binary into those bits: 1100, with zeros filling the rest of the 32 bits. The variable holds the value directly, which is why int is a value type.
In C#, your code is first compiled to intermediate language (IL); when the program runs, the CLR’s just-in-time (JIT) compiler turns it into machine code, and the names you chose become memory locations.
A reference type in memory: string name = “Sunil”
A reference works like giving someone’s address instead of the person: you say where to find them.
For string name = "Sunil", the text “Sunil” is stored in a separate memory area called the heap. The variable name, a local variable on the stack, holds only the address of that text. That is why string is a reference type.
Two variables, one object
Because a reference-type variable holds an address, copying it copies the address, not the object:

Student obj1 = new Student();
obj1.Name = "Sunil";
obj1.Age = 16;
Student obj2 = obj1; // obj2 gets the same address
obj1.Name = "ABC";
Console.WriteLine(obj2.Name); // ABC
obj1 and obj2 point to the same object, so a change made through one is seen through the other. With a value type, int b = a; copies the number, and changing b never changes a.
Video chapters
- 0:00 What is a variable?
- 0:53 Input, process and output
- 1:33 Vary + able: why we need variables
- 3:18 Value type and reference type
- 4:40 int num1 = 12 in memory
- 5:29 Memory stores 0s and 1s
- 5:52 16 in binary: 10000
- 6:40 An int takes 32 bits
- 8:12 The name maps to an address
- 9:42 Behind the scenes: IL, CLR, JIT
- 11:16 Reference type: it stores an address
- 12:08 String name = "Sunil": stack and heap
- 13:16 Two objects, one reference
- 15:38 Wrap-up
Watch the short versions
Short on time? These Shorts from this lesson each make one point in a minute or so.
- 16 in Binary Is 10000: Read the Remainders Up (Short, coming soon)
- Two Variables, One Object: How References Work (Short, coming soon)
- What Is a Variable? Vary + Able, Explained (Short, coming soon)
Key takeaways
- A variable is a named place in memory whose value can change (vary + able).
- A value type (int, double, bool) holds its value directly; an int is 32 bits.
- Memory stores bits; 16 in binary is 10000, read from the bottom up.
- A reference type (string, objects) holds an address; the data lives on the heap.
- Copying a reference makes two variables point to one object.
Frequently asked questions
Is string a value type or a reference type?
In C#, string is a reference type: the variable holds a reference to the text on the heap. Strings are immutable, so changing a string variable makes a new string instead of changing the old one.
How many values can a 32-bit int hold?
A signed 32-bit int holds whole numbers from -2,147,483,648 to 2,147,483,647 (that is, up to 2^31 – 1).
Does each memory address hold one bit?
No. Memory is addressed per byte (8 bits). A 32-bit int takes 4 bytes, at 4 consecutive addresses.
Further reading
- Value types (C# reference, Microsoft Learn): what a value type is
- Built-in reference types (C# reference, Microsoft Learn): string, object and how references are copied
- Integral numeric types (C# reference, Microsoft Learn): sizes and ranges of int, long and others
- Managed execution process (.NET, Microsoft Learn): C# to IL to machine code with the JIT
Practice: write 13 in binary, then predict: int a = 5; int b = a; b = 7; what is a now?
Subscribe and follow the series from the start on the course page.
