What is the purpose of IDisposable?
π‘ Concept: IDisposable Interface
IDisposable is an interface in C# used to release unmanaged resources like file handles, database connections, or unmanaged memory.
π Quick Intro
Implementing IDisposable ensures deterministic cleanup of resources that the garbage collector doesnβt handle.
π§ Analogy
Think of IDisposable as a contract that promises to clean up after yourself, like returning library books after reading.
π§ Technical Explanation
- π§Ή IDisposable defines a Dispose() method for resource cleanup.
- βοΈ Used to free unmanaged resources deterministically.
- π Often used with the using statement for automatic disposal.
- π οΈ Helps prevent resource leaks and improves application stability.
- π Classes managing unmanaged resources should implement IDisposable.
π― Use Cases
- β Closing database connections.
- β Releasing file handles.
- β Cleaning up unmanaged memory.
- β Ensuring timely disposal of resources.
π» Code Example
public class ResourceHolder : IDisposable {
private bool disposed = false;
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (!disposed) {
if (disposing) {
// Free managed resources here.
}
// Free unmanaged resources here.
disposed = true;
}
}
~ResourceHolder() {
Dispose(false);
}
}

β Interview Q&A
Q1: What is IDisposable used for?
A: To release unmanaged resources.
Q2: How does Dispose() work?
A: It frees resources deterministically.
Q3: What is the using statement?
A: A syntax sugar for IDisposable to auto-dispose.
Q4: When should a class implement IDisposable?
A: When it uses unmanaged resources.
Q5: What does GC.SuppressFinalize do?
A: Prevents finalizer from running if already disposed.
Q6: Can Dispose be called multiple times?
A: Should be safe and idempotent.
Q7: What is finalizer (~ClassName)?
A: Backup cleanup if Dispose not called.
Q8: Why use IDisposable with unmanaged resources?
A: GC doesnβt handle unmanaged resources.
Q9: What happens if Dispose not called?
A: Resource leaks and issues.
Q10: How to implement Dispose pattern?
A: Use Dispose(bool) with suppression.
π MCQs
Q1. What is IDisposable?
- Class
- Interface for cleanup
- Method
- Exception
Q2. What does Dispose() do?
- Creates objects
- Frees resources
- Handles errors
- Manages threads
Q3. What is the using statement?
- Loop
- Auto-dispose syntax
- Exception handler
- Class definition
Q4. When to implement IDisposable?
- Always
- Never
- When using unmanaged resources
- Only in console apps
Q5. What does GC.SuppressFinalize do?
- Runs finalizer
- Prevents finalizer
- Creates GC
- Disposes object
Q6. Can Dispose be called multiple times?
- No
- Should be safe
- Sometimes
- Never
Q7. What is a finalizer?
- Constructor
- Backup cleanup
- Method
- Property
Q8. Why use IDisposable?
- Ignore resources
- Handle unmanaged resources
- Optimize CPU
- Log errors
Q9. What if Dispose not called?
- No effect
- Resource leaks
- App crash
- GC fixes
Q10. How to implement Dispose pattern?
- Use Dispose(bool)
- Ignore
- Use finalizer only
- Throw exceptions
π‘ Bonus Insight
Proper implementation of IDisposable is crucial for robust and resource-efficient C# applications.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!