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 Implementing a Dispose Method.
In This Section
- Implementing a Dispose Method
Describes the implementation of the Dispose method for releasing unmanaged resources.
- Overriding the Finalize Method
Describes the way the Finalize and Dispose methods work together.
- Destructor Syntax in C# and C++
Describes the C# and C++ equivalents of the Finalize method.
- Using Objects That Encapsulate Resources
Describes ways to ensure that the Dispose method is called, such as the C# using statement (Using in Visual Basic).
Reference
- IDisposable
Defines the Dispose method for releasing unmanaged resources.