|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Cleaning Up Unmanaged Resources
You should prevent users of your application from calling an object's Finalize method directly by limiting its scope to protected. In addition, you are strongly discouraged from calling a Finalize method for a class other than your base class directly from your application's code. To properly dispose of unmanaged resources, it is recommended that you implement a public Dispose or Close method that executes the necessary cleanup code for the object. The IDisposable interface provides the Dispose method for resource classes that implement the interface. Because it is public, users of your application can call the Dispose method directly to free memory used by unmanaged resources. When you properly implement a Dispose method, the Finalize method becomes a safeguard to clean up resources in the event that the Dispose method is not called. For more information on correct implementation, see .
In This Section
Describes the implementation of the Dispose method for releasing unmanaged resources.
Describes the way the Finalize and Dispose methods work together.
Describes the C# and C++ equivalents of the Finalize method.
Describes ways to ensure that the Dispose method is called, such as the C# using statement (Using in Visual Basic).
Defines themethod for releasing unmanaged resources.