15. Value Type & Reference Type
Understanding Memory Structure
In C#, data is divided into Value Types and Reference Types based on how it's stored.
Stack and Heap
Stack
- Last In First Out (LIFO): Last in, first out
- Where value type data is stored
- Fast access speed
- Suitable for small data
Heap
- Where reference type data is stored
- Data that can dynamically change in size
- Accessed through memory address (reference)
Value Type
Value types are stored in Stack and store data (value) directly.
Value Type Characteristics
- Directly copies data
- When assigning value to variable, the value itself is copied
- Changing one variable doesn't affect others
Value Type Example
int a = 5;
int b = a; // Value of a (5) is copied to b
b = 10; // Only b changes to 10
Debug.Log(a); // Outputs 5 (a is not changed)
Debug.Log(b); // Outputs 10
Value Types in Unity
Vector3(struct)- Basic types like
int,float,bool - Structs
Vector3 position1 = new Vector3(1, 2, 3);
Vector3 position2 = position1; // Value is copied
position2.x = 10; // Only position2 changes
// position1 is still (1, 2, 3)
Reference Type
Reference types are stored in Heap and accessed through memory address (reference).
Reference Type Characteristics
- Stores memory address
- When assigning to variable, references the same memory address
- Changing one variable affects others
Reference Type Example
int[] myArray = { 1, 2 };
int[] myArray2 = myArray; // References same memory address
myArray2[1] = 7; // Change myArray2
Debug.Log(myArray[1]); // Outputs 7 (myArray is also changed!)
Debug.Log(myArray2[1]); // Outputs 7
Reason: Both arrays point to the same memory address.
Reference Types in Unity
GameObject(class)string- Arrays (
int[],string[]) - Lists (
List<T>) - Classes
GameObject obj1 = new GameObject("Player");
GameObject obj2 = obj1; // References same object
obj2.name = "Enemy"; // Change obj2's name
// obj1.name also becomes "Enemy"!
Difference of new Keyword
new in Value Types
Vector3 pos = new Vector3(1, 2, 3);
- Creates value in Stack
- Simply sets initial value
new in Reference Types
GameObject obj = new GameObject();
- Creates object in Heap
- Performs memory allocation and initialization
Value Type vs Reference Type Comparison
| Feature | Value Type | Reference Type |
|---|---|---|
| Storage location | Stack | Heap |
| Stored content | Value itself | Memory address (reference) |
| Copy method | Value copy | Reference copy |
| Change effect | Independent | Shared |
| Examples | int, Vector3 |
GameObject, string[] |
Practical Usage Examples
Value Type Usage
// Store player position
Vector3 playerPos = new Vector3(0, 0, 0);
Vector3 savedPos = playerPos; // Value copy
playerPos.x = 10; // Only playerPos changes
// savedPos is still (0, 0, 0)
Reference Type Usage
// Multiple scripts share same list
List<int> scores = new List<int>() { 100, 200 };
List<int> highScores = scores; // References same list
highScores.Add(300); // Add to highScores
// 300 is also added to scores!
Notes
- Reference types share memory address, so changing one affects others
- Value types copy the value itself, so each works independently
- Be careful when using
GameObjectin Unity as it's a reference type