Verified Jobs Live
.NET Medium Level ~3 min read

Why does garbage collection not remove the need for IDisposable?

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

Garbage collection reclaims managed memory that is no longer reachable, on a schedule determined by the runtime. IDisposable provides deterministic cleanup for resources such as file handles, database connections, and wrappers around native allocations. Use using or an appropriate ownership patte so cleanup occurs on normal and exceptional exits. Disposing an object does not necessarily mean its managed memory is immediately freed. Objects should document whether they own resources, and callers should not dispose dependencies whose lifetime is managed elsewhere.