Hereâs a neat little thing about Visual Studio 2005 IDE for Visual
Basic.Net. When you implement the IDisposable interface, the
following code is automatically created for you. This is
different from other interfaces in that only the code stubs/shell (empty methods)
are produced for other interfaces. The auto-cdoe for IDisposable
actually produces the code needed for the entire implementation except
for the actual cleaning up of resources. The private fields,
Public Dispose Sub and Finalize come complete with no needed
modifications so that nothing gets left out. However, if you
forget to clean up a managed or unmanaged resource in the Private
Dispose(bool) method, that's your own fault :). Update: As Blair mentioned in the comments, for C# IDE you can right click on the IDisposable term and pick if you want to explicitly
or implictly fill in the stubs for the interface.
VB.Net 2.0 in 2005 IDE IDisposable implementation
Public Class MyClass Implements IDisposable
Private disposed As Boolean = False
' IDisposable Private Overloads Sub Dispose(ByVal disposing As Boolean) If Not Me.disposed Then If disposing Then ' TODO: put code to dispose managed resources End If
' TODO: put code to free unmanaged resources here End If Me.disposed = True End Sub
#Region " IDisposable Support " ' This code added by Visual Basic to correctly implement the disposable pattern. Public Overloads Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub
Protected Overrides Sub Finalize() ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(False) MyBase.Finalize() End Sub #End Region