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!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: