Use of Value and Reference Type in .NET Core
π‘ Concept Name
Value Type vs Reference Type
π Quick Intro
In .NET Core, value types are stored in the stack and hold actual data, while reference types are stored in the heap and hold a pointer to the data. Understanding both is key to memory management and performance.
π§ Analogy / Short Story
Imagine two people sharing a paper document. One person gives a *copy* (value type), and the other gives a *link* to the original (reference type). With a copy, changes wonβt affect the original. With a link, both see the changes in real time.
π§ Technical Explanation
- Value Types: Directly contain data. Stored on stack. Example:
int, float, struct
. - Reference Types: Store reference to data. Stored on heap. Example:
class, object, string, array
. - Stack memory is faster and short-lived, heap is slower but flexible.
- Passing value types copies data; passing reference types copies reference (pointer).
π― Purpose & Use Case
- β Value types for small, fixed data structures (e.g., coordinates, enums)
- β Reference types for shared or large data (e.g., models, lists, strings)
- β Use value types when mutability isnβt required
- β Use reference types to share and update data across layers
π» Real Code Example
// Value Type
int a = 5;
int b = a;
b = 10; // a is still 5
// Reference Type
class Person { public string Name; }
Person p1 = new Person() { Name = "Ravi" };
Person p2 = p1;
p2.Name = "Amit"; // p1.Name is also "Amit"

β Interview Q&A
Q1: What is a value type?
A: A type that holds the data directly.
Q2: Where are value types stored?
A: On the stack.
Q3: Give examples of value types.
A: int, float, bool, struct.
Q4: What is a reference type?
A: A type that stores a reference to the actual data.
Q5: Where are reference types stored?
A: On the heap.
Q6: Are strings value or reference types?
A: Reference types (but immutable).
Q7: What happens when you pass a value type to a method?
A: A copy is passed.
Q8: Do reference types support inheritance?
A: Yes.
Q9: What is boxing?
A: Converting a value type to object (reference type).
Q10: Can a struct inherit from another struct?
A: No, structs donβt support inheritance.
π MCQs
Q1. What is a value type stored in?
- Heap
- Stack
- Cache
- Queue
Q2. Which of these is a reference type?
- int
- bool
- struct
- string
Q3. What happens when a reference type is assigned to another?
- Copied by value
- No assignment allowed
- Both point to same memory
- It crashes
Q4. What does 'boxing' mean in .NET?
- Object to class
- Value type to reference type
- Method to function
- None of these
Q5. Which keyword is used to define a value type?
- class
- struct
- interface
- enum
Q6. Which memory is used for large or dynamic data?
- Register
- Stack
- Heap
- ROM
Q7. What happens when you pass a value type to a method?
- Reference is passed
- A copy is passed
- Pointer is passed
- Link is passed
Q8. Are classes value or reference types?
- Value
- Reference
- Hybrid
- None
Q9. Which of these types is immutable?
- int
- bool
- string
- object
Q10. Can reference types be null?
- No
- Only in C++
- Yes
- Only structs
π‘ Bonus Insight
Use structs (value types) only when you need small, lightweight, immutable data. Overusing value types for large data can negatively impact performance due to copying overhead.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!