What is IDisposable and the using statement?
Answer
IDisposable is an interface with a single method void Dispose() used to release unmanaged resources (file handles, database connections, network sockets, COM objects) that the GC cannot automatically clean up. The using statement ensures Dispose() is called even if an exception is thrown: using (var conn = new SqlConnection(connStr)) { conn.Open(); /* ... */ } — equivalent to a try/finally block. Using declaration (C# 8+): using var stream = File.OpenRead("file.txt"); — disposed at the end of the enclosing scope. Implementation pattern: public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }. Include a finalizer as a safety net for unmanaged resources: ~MyClass() { Dispose(false); }. The Dispose Pattern distinguishes between managed and unmanaged cleanup. Always call Dispose() (or use using) on IDisposable objects.