Advertisement
Intermediate C#, Memory & Garbage Collection

What is the IDisposable pattern and why implement it?

Short answer

It provides deterministic cleanup of unmanaged resources via Dispose(), usually with using. Implement it when a type owns unmanaged or disposable resources.

Save C# mock interview
Advertisement
AModel answer
It provides deterministic cleanup of unmanaged resources via Dispose(), usually with using. Implement it when a type owns unmanaged or disposable resources.
public void Dispose() { _conn.Dispose(); GC.SuppressFinalize(this); }
Real-world example
A repository that owns a DbConnection implements IDisposable so callers can release it promptly.